Items for Inventory

Hello Everyone,
I am attempting to create some ‘reusable assets’ for my games and one of them will be set of scripts and prefabs for inventory management. I know there are plenty of tutorials - but all of them do things differently. I am more interested in ‘professional design’ of the feature than actual implementation.

I am having a Scene where several items are lying on the “floor”. Some of them are wearable (in different slots - armor, weapon etc.), some of them are usable (potions, spells etc.), some of them unlock access (keys).

Now what I am trying to achieve:

  1. Create an inventory system with slots
  2. Player character has inventory but so do enemies/NPCs(however they do not pick up the items)
  3. I am trying to minimize the number of necessary scripts, keep it nice and tidy

My current idea for the approach is:

  1. Have a Inventory script that can be attached to any character. The Script holds collection of Items (kind of backpack), slots for armor/weapons/shield
  2. Have a InventoryManager having methods for collecting items, throwing away items, quick slots etc.
  3. Generic Item script that holds all the objects description (name, type, slots that the items can fit in, all the details (defense, attack, path to icon sprite, path to sprite when used), also some methods like ‘use’, destroy’.
  4. ItemFactory that holds ‘details of all items’ and uses them to Instantiate the actual Items.

Here are my questions:

  1. Is this type of design a good practice? Is there something I am not seeing? Any problem I may be running into?
  2. Should I keep the data of the objects in files and load them at the beginning of the Scene?
  3. What Design Patterns should I look into around the inventory system? Is there any particular one that is generally used for things like that?
  4. When the player grabs items from the floor, should I destroy the game object and create ‘simplified’ version to put in the collection or rather keep it all the time without destroying?
  5. Is it better to use generic class “Item” that has the whole state with parts that are not relevant (e.g. attack for armor) or should I rather split the items into separate objects that extend the object Item?

Kind regards,
Piotr

Not sure if this is a good practice since i never worked on an inventory system, but it sounds reasonable.
What i feel more comfortable answering is:

  1. You can either load it all when entering a scene, or load it on demand when it’s actually needed. This also depends on what data we are talking about, and how much. Strings like names and descriptions are no problems, but loading hundreds of images or even meshes may be problematic.
  2. You should really never destroy gameobjects unless absolutely necessary. This causes garbage which needs to be collected, thus causing spikes in frametimes at random intervals. To prevent this look into object pooling. The idea is that you have a pool of objects (for example swords of the same type) and if you would normally destroy this object, you instead return it to the pool. Then you need a new one, you take it from the pool. The pool can also have a dynamic size. The idea is to never destroy (and possibly not even create) objects in Update() to prevent garbage collection.
    Also, having a simplified version (like an icon with a description) in the inventory would be better regarding 2). You would then only need to load these icons + descriptions for example, and if you take them out of the inventory you’d spawn the real item.
  3. I would define a class hierarchy based on inheritance. The base class ‘Item’ would only contain a name, from it derives ‘ArmorItem’ which has a name and an armor value, and so on. It’s rather pointless for all items to have armor values if they dont need them. You may also want to look into ScriptableObjects since they are a bit more lightweight, if you only need to store data.
1 Like