Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


[resolved] Inventory System 'Architecture'


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Orymus3   Crossbones+   -  Reputation: 9959

Like
0Likes
Like

Posted 10 July 2014 - 09:23 PM

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, 14 July 2014 - 10:14 PM.


Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 7797

Like
6Likes
Like

Posted 10 July 2014 - 10:57 PM

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 ?

 

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).



#3 SeanMiddleditch   Members   -  Reputation: 6355

Like
3Likes
Like

Posted 11 July 2014 - 12:25 AM

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.).

#4 arka80   Members   -  Reputation: 1006

Like
2Likes
Like

Posted 11 July 2014 - 01:40 AM

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, 11 July 2014 - 01:41 AM.


#5 Thinias   Members   -  Reputation: 139

Like
3Likes
Like

Posted 11 July 2014 - 01:47 AM

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, 11 July 2014 - 01:49 AM.


#6 Orymus3   Crossbones+   -  Reputation: 9959

Like
0Likes
Like

Posted 11 July 2014 - 04:50 AM


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!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS