Creating a Simple Inventory System

Posted by Josiah Purtlebaugh on Monday, August 24, 2020


Creating The Inventory Data Structure

After implementing doors and allowing the player to open and close them, it occurred to me that adding keys and a locking mechanism would be enough to breathe life into the first solid game mechanic of my current prototype. With that in mind, I realized that in order to unlock a door the player must be carrying a key, so I must first implement some simple inventory management. I’ll describe below how I went about it.

First, I took stock of the requirements of an inventory system:

  • Display items (static meshes) in the game world that are interactable
  • Upon interaction, add the item to the player’s inventory for tracking (and hide the item model)
  • Upon interacting with another object, optionally use the item from the player’s inventory and affect the target

The first step was the model the system of blueprints to describe an inventory and items. We must begin with the simplest components and build up, so I started with an enum to describe item types, called ItemTypes. You can create an enum from the Content Browser via Add New -> Blueprints -> Enumeration. I added a single item called KEY because the type of item I wanted to add first was a key.

Please note, my system language is in Japanese so your screen may look slightly different.

ItemTypes Enum

After adding the ItemTypes enum, it was time to add a new struct to describe an item. I created a struct (Content Browser -> Add New -> Blueprints -> Struct) with the following fields.

  • Type of type ItemTypes, to store what type of item this is
  • Name of type Name, to store the item name
  • Amount of type Integer, to store the amount of items (perhaps this is a stack of two keys)
  • StaticMesh of type StaticMesh, for future reasons (such as if the player drops the item later)

Item Struct

With the Item struct implemented, I finally created the Inventory struct (Content Browser -> Add New -> Blueprints -> Struct). The Inventory struct is simple as it contains only an Array of Items. Be sure to click the little blue dot next to the dropdown box to set it to Array.

Inventory Struct

Before moving on, quickly head over to your Player Blueprint and add the Inventory variable and just call it Inventory.

Creating A Base Item

Next, we will need to create the blueprint upon which all items will be based. I called mine Item_BP, and I created it via Add New -> Blueprint Class. I chose to use the Actor type. Start by adding a simple static mesh and setting it as the scene root. Next, we’ll add a new variable and set the name to Item and the type to Item. In order to use the static mesh described by the Item struct, I set up my Constructor Script like so:

Item_BP Constructor

Next, head over to the event graph so we can implement the click interaction. In this example there are two custom functions called IsPlayerCloseEnoughToActivate and AddToInventory.

I decided to add IsPlayerCloseEnoughToActivate to a custom function library. You can add this via Content Browser -> Add New -> Blueprints -> Function Library. This could just as easily be implemented directly on the Player Blueprint if you so desire. Essentially this blueprint measures the distance between the current actor and the player and returns whether the object is within ActivateDistance (a float on the Player Blueprint).


As for AddToInventory, this was implemented directly on the Player Blueprint. Very simply, it takes a target Actor (which is cast to Item_BP and adds the relevant data to the Player’s Inventory variable).


Finally, we can build out the event graph for Item_BP. A very simple blueprint that when clicked checks whether or not the player is within range. If so, it adds the item to the player’s inventory and destroys the Item_BP from the game world.

Item_BP Event Graph

Before moving on to using items from the inventory, let’s create Item_Key_BP which will allow us to place the key in the scene. Right-clicking on Item_BP shows the option to create a child class, so let’s do that and name it Item_Key_BP. In the Blueprint Editor, click on Item_Key_BP(Self) in the top left and on the right you will see the Blueprint details. A section labeled Default will show the Item variable we created on Item_BP. Expanding this will allow you to set the details for this Blueprint. Thanks to the Item_BP Constructor we created above, setting the StaticMesh variable here will allow the model to show in the scene immediately.

Item_Key_BP Details

After you’ve created Item_Key_BP, place an instance into your scene and the model should appear.

Implementing Item Interaction

To demonstrate interaction using items we will allow the key to unlock a door that has been placed in the scene. I will gloss over the details regarding the full door implementation and focus on the item interaction. In order to allow items to be used, we will add a new function to the Player Blueprint called ConsumeItem. Upon being executed this code will simply delete the item from the player’s inventory.


Below is a screenshot of the primary Event Graph for my Door Blueprint. In this Blueprint I have utilized the IsPlayerCloseEnoughToActivate function again to check the distance from the player. If close enough, another custom function called UnlockIfPlayerHasKey is called. From there, the Blueprint plays an opening or closing animation.


Let’s take a closer look at UnlockIfPlayerHasKey below. In this custom function, we first checked to see if the door isLocked. If not, we return false which allows the door to open. If true, we cast the current Actor to Item_BP and call a simple custom function called HasItemType to determine if an item of type KEY is in the Player’s Inventory. If there is a KEY, we call ConsumeItem and set isLocked to false. If there is no key, we call a custom event called Display Message and set isLocked to true (not strictly necessary, but it seemed to add clarity to the example). HasItemType, show below, is implemented on the Player Blueprint and just loops through the inventory and returns the first item of matching type. Otherwise, it returns false.



The last quick piece of the puzzle is adding the custom event called Display Message. This can be done directly in the Door Blueprint by right-clicking and adding a new custom event called Display Message. I’ve set it up below so that it displays a message for 3 seconds and then hides the message again.


The End Result

I ended up with the result shown in the video below. The message is displayed when clicking on the door, the key is picked up, and the door is finally unlocked after picking up the key.

Final Thoughts

There are a lot of improvements that could be made to this very simple system, but I wanted to focus on my first iteration as I continue to learn Unreal Engine 4. If something here is unclear or incorrect then please reach out to me via twitter and I will be happy to clarify or update the tutorial.