No announcement yet.

Creating/Storing large amount of variables (Inventory system).

  • Filter
  • Time
  • Show
Clear All
new posts

    Creating/Storing large amount of variables (Inventory system).

    Hi I am currently brainstorming a good way to set up my games inventory system.

    One of the challenges I always face when creating inventory systems for games with heaps of different items is how I am going to store what items and how many of them are currently in the inventory.

    I would like to share my favorite method I use for this. Let me know what you think about this method and if you know of a better way.

    My favorite method is storing the data on the inventory icon itself. One of the things that always comes to mind when thinking about inventories is how I can make the variable that holds the amount of the item the player has only exist if the player actually has at least one of those items. So for example, I would create the inventory icons as prefabs/BP's that have a struct containing int ID, int Amount, bool isStackable etc and then store the prefabs/bps in a dictionary where their ID is the key value so they are easy to grab when they need to be instantiated.

    At runtime none of these icons exist in the scene/level. The objects in the scene that the player can get these items from like harvestable nodes or pickup items hold the same struct as the icon. So when the player picks up/harvests an item the game will check the ID in the struct that is on the pick up item and then check if any of the icons in the players inventory has a matching ID. If it does find an icon with a matching ID it will then check if it is Stackable, if true then it will simply add the items picked up to the amount variable attached to icon in the inventory, if it is not stackable or does not exist in the inventory it will send the ID from the picked up item to the Dictionary that holds the icon prefabs/bps and get the prefab/bp it needs to instantiate in the inventory and instantiate it in an empty slot.

    I have had no issues using this method but I have heard from some people that it is a bad idea to store data on objects in the scene/level unless the object is static and will never be destroyed/instantiated.

    Does anyone have any opinion on whether my method is a good or bad idea?