Part 7: The Inventory
A basic part of the mechanic in our project is carrying and using various objects. In order to do this, today’s diary entry will deal with the inventory. We would like to see a number of storage slots on our screen into which we can place our items. It should be possible to rearrange items via drag and drop. In addition, we want to have the possibility to combine items with each other or stack them. Some items will be used directly by tapping them.

In line with the current development progress of the project, the first items we create will be designed to start our first mini game GONG!, which we came up with in the last diary entry. For this, our player is supposed to carry a portable console on which the game is then started. In addition to the console we need the mini game itself in the form of a cartridge as well as a suitable power supply in the form of batteries. My idea for the first console is a kind of GameBoy in which you have to insert a game and two batteries to use it. For copyright reasons I call this handheld "GameGuy". If this GameGuy is tapped, a window opens which offers more slots that will only allow one game module and two batteries to be placed in them.

You should not reinvent the wheel
Of course there are already many games that use a kind of inventory - it is nothing extraordinary. For this reason, it is a good idea to look for a finished implementation that you can use first, rather than reinventing everything from scratch. Even if you want to do everything yourself, there is no harm in looking at other variants for inspiration.

Unity provides the so-called AssetStore which provides a variety of resources for your own game development. Here we find graphics, audio files, 3D models and also entire engines and systems such as inventory systems. Fortunately, you won’t have to pay an arm and a leg to afford them - there are a lot of free offers as well. After some searching and comparing, I found the package "Inventory Master - uGUI". This allows us to use various typical inventory components. For example, there is a main inventory, a kind of quick access bar and a crafting system. There is also a database in which you can create new items. Drag and drop functionality is already available. I imported this package into my project and inspected it more closely. The inventory system turns out to not quite offer what I imagined but it could be used as a solid foundation. In the end, though, I've changed so much that maybe it would have been easier to make it all myself but I could learn a lot and adapt some mechanisms.

The implementation of the inventory has confronted me with many challenges and was a long-winded process. Nevertheless, I would not like to go deeper into the matter and rather report on the results. In the end, my item database currently contains the following items: A GameGuy console in two variations, a game cartridge with GONG! and a 1.5V AA battery. Each type item has a small picture of size 64x64 pixels. They are subject to a certain type-hierarchy in order to simplify and abstract various logical processes later on. This is called inheritance. For example, the GameGuy is a console or even a handheld and an AA battery is a special type of "battery". This helps with certain checks. For example, you can only insert certain batteries and data carriers into a (portable) console. If you try this, it is checked in detail whether this type of battery fits into this type of console. This way I can handle logical queries hierarchically and effectively as well, rather than checking out large, confusing lists of possibilities in the end.

After that I created a GUI (a window) which offers three slots as described above. The first one only allows GameGuy cartridges to be placed in them while the other two are reserved for one AA battery each. Only when these slots are filled, a start button is activated with which the mini game can be executed.

Inventory bar with open GameGuy interface including the game and batteries.

The game cartridge can be linked to one of the mini games and accordingly carries a suitable cover image - as seen in the picture above. The batteries also need a charge. This is represented by small bars next to the item. While using the console, this charge is slowly depleted. If at least one of the batteries is empty, the console will turn off automatically. A small battery indicator prevents this from happening unnoticed during play.

The batteries and the game cartridge can either be dragged into the slots of the console window or directly to the console. In both cases one of the corresponding slots still must be empty, of course. If you drag an item into an already occupied slot, the two items exchange their positions. An exception are stackable items which can be combined into one slot but I don’t plan to add anything like that for now. At least batteries aren’t viable for stacking because their different charges would make it rather confusing.

With this we now have a solid inventory system which prove itself as an important component throughout the entire project. In the following diary entry, we will deal with saving and loading of our progress since we are able to carry out first changes of states now which we want to keep, of course.

Do you prefer to read this diary in the developer's mother tongue? Then click here to read this diary entry in the original German language!