Inventory
A basic inventory system based on IInventory interface.
Last updated
Was this helpful?
A basic inventory system based on IInventory interface.
Last updated
Was this helpful?
The inventory system is built on two lists: Items and Collectables.
Items is a list of InventoryItem
objects, which extend MonoBehaviour
and are therefore attached to GameObjects.
Collectables track the quantity of each inventory item, maintaining count data independently.
The Items list updates based on the children of the inventory GameObject. If the inventory has 10 InventoryItem
children and one regular GameObject, the list will include only the 10 InventoryItem
components.
Creating an inventory is as simple as attaching the Inventory component to an empty GameObject, which should be a child of the main player GameObject. This GameObject serves as both the parent for all item objects and the manager through the Inventory class.
As explained in the summary above, the inventory automatically identifies all InventoryItem
components that are children of the inventory GameObject. Based on this behavior, if you add a GameObject that extends InventoryItem
, the inventory system will correctly recognize it and manage its activation (enable/disable) accordingly.
To create a custom inventory item, start by creating a new script and set its inheritance to InventoryItem
instead of MonoBehaviour
.
This script must be attached to the GameObject you want to include as a child in your inventory. For example, if you're adding a melee weapon, create the melee GameObject, attach the MyItem
(Your Custom Script) to it, and then make it a child of the inventory GameObject. The inventory will then automatically detect it and handle switching to or from it as needed.
To handle picking up and dropping the item, you need to create a prefab from it. In this example, we have a GameObject called Melee that handles melee attacks and has the MyItem
script attached. Create a prefab from this GameObject to use it as your custom inventory item.
This prefab can then be assigned to a Pickable item. When the player interacts with the Pickable, it will add the item to the inventory and automatically switch to it, just like it would with any default weapon.
To drop an item, you first need a Pickable version of it, as shown in the code above. Once that's set up, you can begin coding the item drop functionality. Here's a simple example to demonstrate how it can be done:
Collectables are distinct from InventoryItems
because their primary purpose is to track the quantity of items. For example, when you add a custom health kit item and use it, you can remove one of the HealthKitCollectables
(A custom one you made). If there are no collectables left, the health kit will be destroyed (In your health kit logic), preventing further use. This same approach is used to track the count of grenades and ammo for firearms, ensuring that their quantities are accurately managed.
You can create any of the default Collectables by opening your inventory component and clicking the "+" icon under the Collectables list.
As shown above, a new Collectable element has been added to the Collectables list, with three fields: Count, Limit, and Identifier.
Count represents the current quantity of this collectable item.
Limit indicates the maximum number of items this inventory can hold.
Identifier is used to distinguish the collectable, determining whether it’s for HealthKits, Ammunition, or another item type.
To create an identifier, right-click anywhere in the project window and navigate to the menu: Create > Akila > Inventory Collectable Identifier
This will generate a scriptable object file for the inventory collectable identifier in your project, which you can then assign to the "Identifier" field.
At this point, only the inventory recognizes the identifier. However, to make use of it, you need something that interacts with it. In code, you first need to locate the collectable in the inventory's collectables list.
With this code, pressing T calls the Heal
function, which subtracts one from the corresponding item in the inventory's collectables list. Once the count reaches zero, the function exits early and no action is taken.
While collectables might seem unnecessary at first, they serve an important purpose. For example, they allow guns that share the same ammo type to maintain a unified "Ammo Left" count across all weapons. In the case of grenades, the grenade item is added only once, and if one already exists, its collectable count is simply increased—making it much easier to manage complex items consistently and efficiently.
To pickup a collectable item:
Set the Type: Change the pickable's Type to Collectable.
Assign the Identifier: In the Collectable Identifier field, assign the identifier you created for this collectable (e.g., HealthKit, Ammo, etc.).
Set the Amount: Enter a value in the Amount to Collect field to define how many of this collectable should be added to the inventory's count when picked up.
Make sure the same Collectable Identifier is also included in one of the elements within the inventory's Collectables list. This ensures the collected amount is properly added to the correct collectable entry.