Hello and welcome to my Inventory system tutorial.
This guide will walk you through the steps on how to make a simple Inventory system.
I will split this tutorial in 2 different guides: super Simple System and a bit more Complex System.
Let’s start!
I will use UE 5.2, Third Person template.
Part 1. Simple System.
Step 1. Preparation.
First of all we need to create a backbone for our Inventory system.
We will need: Pick up actors, Use functions, array to keep our inventory, Enum and Struct to make it easier to differentiate between items.
Create a new folder. There we are going to keep our items. For a cleaner look, I will also create separate folders for each Pick up and its Use function.
First let’s create Enum.
Add → “Blueprint” → “Enumeration”.
I’ll call it “ItemType” and add items to the list. For the sake of simplicity I’ll only have 2 items: None and “Healing Potion”.
Next, we need to keep our inventory somewhere. We will use custom Game Instance for this job. I’ll call it simply “GameInstanceCustom”.
After we created it, we need to tell our engine what game instance it should use.
On the top left corner, press “Edit” → “Project Settings…”. Search “game instance”. Change Game Instance Class to ours.
Now, let’s add Inventory array. This is a super simple Inventory, so we just need to know what type of items we have. For this we will use the previously created Enum “ItemType”.
Add the variable “Inventory” to our game instance, set its type to “ItemType” and make it into an array.
Next, we need Master classes.
Create a new object, let’s call it “UseFunction_Master”. We will need it to create child classes that will have functionality, which we will execute from our Inventory.
To make our Use function actually usable we need to add a new custom event and reference to its user (player).
That’s it for our Use function Master.
Next, Pickup.
Create a new actor, let’s call it “PickUp_Master”. We will need it to create child classes to place them in the world for players to pick up.
For our Pick up we need 2 components: Mesh and Collision Hull (box, sphere or capsule, whatever you like).
We need to edit their collision settings.
For the mesh we don’t need any collision, so just change its preset to “NoCollision” and make sure to uncheck Generate Overlap Events.
For the collision we can keep it the same, but for a bit cleaner job I will make it overlap only with pawns.
For easier work with the child classes let’s add variables for our mesh, its material and to keep Use function class.
- “PickUpMesh”, Type: Static Mesh;
- “Material”, Type: Material;
- “Type”, Type: ItemType.
Press the eye icon on them, to make them Instance Editable.
Now we need to add code to construct our pickup using our variables.
Go to Construction script and add this code:
Let’s add some code to actually pick up our future items.
The logic of the code:
Actor Overlapped by someone → Check if it was the player by casting to the character → Get Game Instance and cast to our custom Game Instance → Get Inventory from it → Add Item to Inventory using created variable type → Destroy Actor.
And at last finishing touch.
Struct. Name it “ClassStruct”.
Add → “Blueprint” → “Structure”.
We need 3 variables in it.
Return to “GameInstanceCustom”.
Add a variable, let’s name it “List”. We will use it as a reference sheet. Set type to “Item Type”. After that, make it into a map. Secondary type will be our “ClassStruct”.
When we are going to add an actual item, we will just update this list.
Step 2. Inventory Widgets.
To see and interact with our inventory we will use User Widget.
We need to create 2 Widgets. One for the Inventory and other for the slots for the Inventory. I’ll name them “Inventory” and “ItemSlot”.
Add → “User Interface” → “Widget Blueprint” → “User Widget”.
First we’ll work on a widget to hold our Inventory.
This is my setup. I’m using Grid Panel to keep ItemSlots.
Let’s add some code.
First we need to construct our inventory. Also I’ll add a custom Rebuild event.
I will set a maximum column number of 5.
Note that “Item Index” and “Parent” won’t be visible for you, we will add them later.
Exit button.
Now let’s work on “ItemSlot”.
First, we will set size for our Slot. On top right of the grid change “Fill Screen” to “Desired”.
Now let’s add components, we need:
-
Size Box (set Width and Height as you want, I’ve set it to 200);
-
Canvas Panel;
-
Background Image;
-
Item Image;
-
Button to Use;
-
Button to Drop;
-
Vertical box to keep buttons (set its visibility hidden);
-
(Optional) Background Blur.
Now let’s add the Inventory Index variable. It will keep the index of our Inventory Array. Make it Instance Editable and Expose on Spawn.
Also we need reference to the Parent widget.
Return to “Inventory” widget and connect Item Index and Parent. If they won’t show up, set class to None and back to Item Slot.
Return to “ItemSlot”.
Now let’s add some code.
First we need a reference to the Game Instance to get access to our Inventory Array. We will get it on Construction of the Slot.
Now we need to set ItemImage. Go back to the Designer, select ItemImage, go to Brush, press “Bind”, Create Binding. We will use the Index variable to get Item Type from Inventory Array and Image from the List. From that we will select what image to use.
Return to Event Graph. First I will add code to show and hide buttons.
Go to Functions and Override “Event On Mouse Enter” and “Event On Mouse Leave”.
Next, “Use” button.
“Drop” button. Note that it will simply spawn Pickup in front of the player. Making better system is on you.
And that’s it.
All that is left is to add Open inventory code to the character.
Step 3. Adding actual Item.
For an example I’ll create a Healing Potion.
Right Click on PickUp_Master → Create Child Blueprint Class.
I’ll simply call it “HealingPotion”.
Open it and close it. Open it again, it should look like this:
As you can see I’ve already set my Defaults.
Now let’s create a Use function for it.
Right Click on UseFunction_Master → Create Child Blueprint Class.
I’ll call it “UF_HealingPotion”.
Open it, then go to Functions and Override → Execute.
We can make whatever we want, but I’ll just add 20 to my HP.
And to finish, update the List in the Game Instance.
Part 2 is coming soon… (maybe).