Killmates – How we created the generic inventory system in Unity 3D

During the developpement of the game Killmates, we were facing of how we’ll handle our weapon inventory or our equipment inventory (and maybe more inventories in the future..). But one thing was clear for us, we don’t want to duplicate our inventory code just to handle different things.
The answer was there : let’s make a generic inventory to store anything.

The final result of our work is this. I’ll write an article about how the UI has been set up but for now, i just will explain you the basis of the generic inventory.

Representation of the inventory

Virtual representation of the inventory In-Memory

The inventory is organized in slots where 1 slot can contains only 1 weapon. In this representation, the number of slots is set to 5 (wich is the number of weapons that a character can hold in Killmates) but of course, you can choose the number of slot that you want.

Inventory features

Here are features we want to provides for the inventory and they must be simple to use above all :

  • Add item
  • Get Item reference (don’t remove the reference within the inventory)
  • Pull the item (Get the reference and remove the item in the slot, let it empty)
  • Create a new slot


The only difficulty we were facing of is “How to retrieve a specific item” identified by it’s name, id, instanceID, … As the inventory is generic, the object identifier will depend of the object.

  • The type of the identifier can change (enums, string, int, …)
  • The name of the variable is never the same from one class to another.

To handle this specificity, we implemented an interface with 2 methods :

  • Compare<T>(T pObjectToCompare)
  • Compare(InventoryQueryIdentifier pIdentifier)

The first one compare one object with an object in the slot. The second one contains the key wich indentifies clearly an object in the inventory.

Interface implementation

The only requirement an object needs to be inventoriable is that it implements the interface IIventoriable.

Here is an exemple for our base class “Weapon” :

#region IInventoriable

/// <summary>
/// Compare with another weapon
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="pObjectToCompare"></param>
/// <returns></returns>
public bool Compare<T>(T pObjectToCompare)
	if (weaponType == (pObjectToCompare as Weapon).weaponType)
		return true;
		return false;

/// <summary>
/// Compare the weapon identifier
/// </summary>
/// <param name="pIdentifier"></param>
/// <returns></returns>
public bool CompareWithIdentifier(InventoryQueryIdentifier pIdentifier)
	if ((eWeapon)weaponType == (eWeapon)pIdentifier.objectIdentifier)
		return true;
		return false;


With these function, our inventory is able to compare object in order to retrieve a specific object if needed.

Exemple of utilisation

/// <summary>
/// Weapon inventory
/// </summary>
public InventoryModel<Weapon> weaponInventory;

public void UsageExemple()
	//Initialisation of the weapon inventory

	//Try to add a weapon in the inventory, if failed, create a new slot and add the weapon

	//Get a specific weapon wich is identified by an enum
	InventoryQueryIdentifier inventoryQuery = new InventoryQueryIdentifier(eWeapon.ENERGYGUN);
	Weapon myRequestedWeapon = weaponInventory.GetObjectWithIdentifier(inventoryQuery);

Here we are ! we can now create a basic inventory for anything like :

  • public InventoryModel<Equipement> equipmentInventory;
  • public InventoryModel<Spells> spellsInventory;

In conclusion, you can use this powerful inventory system with anything !
You can find the inventory on my GitHub just right here : GitHub

Hope you really enjoy it and stay tuned for other articles. Feel free to ask me you questions or your improvement requests.

On the next article, i will show you how to listen to inventory events in order to update the UI for instance. Stay tuned.

Posted in Stories and tagged , .

Leave a Reply

Your email address will not be published. Required fields are marked *