Jump to content
  • Advertisement
Sign in to follow this  
Orymus3

[resolved] Inventory System 'Architecture'

This topic is 1495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

I'm currently toying with an Inventory system for one of my project and I'm not sure on what the best approach is to avoid future hurdles. 

The way it works is that it contains a series of 'components' each with their own ID, which are different types (there are 6 types of components, and any given component is one, and only one type at any given time).

 

Initially, I had created 6 Hashmaps (ordered by an integer, thus the component ID). This allowed me to quickly find all necessary information for a component's specifications when looking up the appropriate INT in the appropriate MAP.

 

As the project progressed, I ended up having a few generic methods in the code that could do with a more fluid approach (for example, creating a button that refers to 'any component' as it would effectively have the same behavior, such as when buying or selling that component).

I've been thinking up two solutions:

 

1 - Merge all of the inventories together and differentiate each component's type by adding a field named along the lines of 'component type'.

 

2 - Create a 'macro' hashmap that contains all of the submaps.

For example

macroMap[0] = componentType1Inventory
macroMap[1] = componentType2Inventory
macroMap[2] = componentType3Inventory
...

#1 seems like the cleanest methods, but #2 feels a lot more organic for what I'm trying to achieve (and prevents using unnecessary getters). I feel I'd get more mileage out of the 2nd approach because it prevents me from having to 'peek inside' any object to figure out if it should be considered or not (if it's in that sub-map, it is de facto meant to be in that list).

 

Any pointers on how I should approach this?

Edited by Orymus3

Share this post


Link to post
Share on other sites
Advertisement
Yeah, it feels like a minor miss on the potential of components. What if an item could be both a weapon and a wand or something like that?

I'd just make each inventory item a regular game object (a collection of components) or an "inventory item composition" and then the inventory can be a list of said objects.

I'm not 100% sure why you think you need separate inventory lists, though it certainly is useful for _some_ types of games. It's possible for any item that can function as an inventory item to contain an InventoryItemComponent that indicates which types of inventories it can be contained in (and some other details like display name key, description key, icon, etc.).

Share this post


Link to post
Share on other sites

I'm using the approach suggested by Ashaman, having an array of items as inventory and keeping a pointer in a secondary data structure for equipped (in use) items: I can have n weapons but only one is the "in use" one. When player change weapon (choosing from a displayed list) I simply overwrite the weaponInUse pointer with the new item address.

Edited by arka80

Share this post


Link to post
Share on other sites

I similarly am not sure that I understood the description of your implementation.  However, I think I would expect to see something which implemented either a base interface or abstract class of "Component" behaviour.  Different component types then differentiate their behaviours through the implementation of their abstract or virtual methods.  When this is done, you don't need to store them in separate lists.  You have one list of "Component" objects, and the components all respond to a common interface in different ways.

Edited by Thinias

Share this post


Link to post
Share on other sites


I don't know if I understood your system correctly, but your component types are, for example, weapons,armor,quest items ? And your components are like a sword, club, helm etc ?

 

Something similar indeed, except for a space game. A "ship" is nothing but a container of components, each of which have their own behavior in my game. So a ship can move because it has engines, can attack because it has weapons, can sustain damage because it has a shield, etc.

The item itself is just the "model" for that component. For all intents and purposes, it behaves as you have suggested.

 





An inventory system is often a very low frequently accessed system (compared to physics,AI,rendering), therefor not really performance critical. In this case I would always sugguest to keep the data-structures and manipulation methods simple,small and clear. Eg a simple array as inventory holding all your components (each component includes its own type) plus some common manipulation methods like add,remove,swap,merge, get next by type, get next by id etc.



On top of this you can build other utility methods, like filtered,sorted views (show me only weapons, sorted by damage), links (button A is linked to item X at inventory index Y).

 

I had not considered this, but you're right. It gets accessed only a few times, and nearly always out of the actual game loop.

 



I'm not 100% sure why you think you need separate inventory lists, though it certainly is useful for _some_ types of games. It's possible for any item that can function as an inventory item to contain an InventoryItemComponent that indicates which types of inventories it can be contained in (and some other details like display name key, description key, icon, etc.).

 

Mainly because of my poor coding practices. I was refactoring something big and ugly (prototype) as I implemented the inventory system (yeah, I did both at the same time for no apparent reason) and wanted to make sure there was no way I would end up assigning an incorrect component type to a "gear" (Equipment) slot by mistake and not understand why everything crashed. That's a very weak reason, but given status quo, I did not revisit this in the last month and as such, it sort of became the standard.

Time to refactor this mess :)

 


I'm using the approach suggested by Ashaman, having an array of items as inventory and keeping a pointer in a secondary data structure for equipped (in use) items: I can have n weapons but only one is the "in use" one. When player change weapon (choosing from a displayed list) I simply overwrite the weaponInUse pointer with the new item address.

 

I have a bit more flexibility. My "hull" component class determines how many of each component types my ship can have (generally more than a single weapon). I build a gear hashmap from this by adding each equipped component. Thus, I can generate a ship on the fly from just a few integers instead of keeping multiple references of ship components all around. This is very light and I like it, but to go to the "next step" I need to revise my approach to inventory management itself (I'm quite satisfied with the equipped part however).

 


I similarly am not sure that I understood the description of your implementation. However, I think I would expect to see something which implemented either a base interface or abstract class of "Component" behaviour. Different component types then differentiate their behaviours through the implementation of their abstract or virtual methods. When this is done, you don't need to store them in separate lists. You have one list of "Component" objects, and the components all respond to a common interface in different ways.

This is what I do. I call them metaComponents. Since this is already done, there are indeed no longer any reason to keep them in separate lists.

 

Thanks all!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!