This is part two of a tutorial series, please first complete one of the following tutorials:
In this part I will show you how to build a basic inventory system to pickup, select and drop items from a player’s inventory. Please note that this tutorial was written based on the C++ Part 1 tutorial, some names or nodes may be slightly different if you work from the Part 1 Blueprint tutorial, but the concepts remain exactly the same.
Building an inventory system is not beginner level. It is expected that you have a basic working knowledge of Blueprint and Unreal Editor. To keep the tutorial of a reasonable size I will not go in-depth for every node, you may of course ask any questions in the comment section!
Download: Tutorial2_Content (Includes post-process for item highlighting)
Project source is available on GitHub!
Built for 4.0. Last updated: 4.7
Building on the UsableActor C++ class from part one we respond to On Used events (“E”-key in tutorial) by spawning a blueprint instance in the character’s inventory and removing the object from the world. When dropping an item (“F”-key in tutorial) we spawn the actor back into the world. I’ve split the inventory and in-world objects into two separate blueprints, this requires a ‘mapping’ between the who which may be undesirable for very large projects, I intend to tackle this issue when moving to a more advanced inventory system (using DataTables) in future parts.
First we create the blueprint classes so we can more easily move through the implementation in a single pass.
Create a new Blueprint based on UsableActor and name it “BP_PickupActor“. This actor is visible in the world and responds to On Used events.
Now create another Blueprint based on Actor and name it “BP_InventoryActor“. This actor holds inventory data such as display name and could hold additional information like item weight and a thumbnail.
Add the following input mappings to your project. The mapped actions are available in Blueprint (We will implement these in the Character blueprint)
This blueprint exists in the world and can be picked up by the player.
Create a new variable “InventoryClass” of type class’Actor’. This variable holds the class that is created when this item is picked up by a player. After creating the variable, compile and assign BP_InventoryActor as the default value.
On Used event handles the response when a player presses “E”-key while hovering over an item. Do note that “Add Item” is a function of our inventory character that we did not create yet. We’ll get back to that later in the tutorial. My character class is named BP_BasicInventoryCharacter in the image below, replace this cast with whatever your Character class is named after (the one that will hold your inventory items)
Tip: If you’re having trouble with “Cast To” nodes, always drag a line from the pin/variable you want to cast.
Begin Focus & End Focus events toggle the “Render Custom depth” on our Static Mesh. If enabled the mesh is drawn into a special depth rendertarget that is used by our custom post process to draw the item outline. For more info on the used on Custom Depth, click here.
Next we assign the defaults for this Actor. Enabling “Simulate Physics” is optional and depends on your game. If you don’t enable physics you may need to adjust your Drop Function later in the tutorial to trace to the floor to find an appropriate drop location instead of letting it fall on the floor.
This blueprint holds item specific information such as display name for the HUD and the class to spawn when dropped.
- DisplayName (type: text)
- PickupClass (type: class’Actor’)
Now compile this blueprint and assign the BP_PickupActor as default value for our PickupClass-variable and set a name for DisplayName or it will not show up in your HUD later in this tutorial.
Compile once more, we’re now done with this Blueprint.
The character owns the inventory array and handles pickup/drop logic.
Add the following variables:
- InventoryItems (type: BP_InventoryActor) as Array, click on the grid-icon next to the type.
- SelectedItem (type: int)
You can set the category to “Inventory” to keep it tidy.
Character Function: SetSelectedItemIndex
Create a new function with an integer as input and name the input “New Index” (see image) This function clamps the selected item index between 0 and the total number of items you have in your inventory. It will be called after dropping an item from your inventory into the world.
Character Function: AddItem
Create a new function called “AddItem“. Now step into this function and select the “Add Item”-node, we must add a new Input “InventoryClass” of type class’Actor’. Remember that our Blueprint BP_PickupActor called this function? Don’t forget to update that function now that the function is available.
Now to ahead and implement the logic of this function as seen below.
We spawn a new inventory actor and add it to the player’s inventory. The Make Transform node is left as default since the inventory item is not visible in the world itself (We could opt for a more base class like Object or a struct that do not hold location variables, for simplicity I chose to use Actor instead)
Tip: To cast the Return value of “SpawnActor” you must drag a line from the ‘Return Value’ and make sure “Context Sensitive” is enabled. Otherwise the “Cast to X” nodes will not show up!
Character Function: DropItem
Create a new function “DropItem“. This function gets the selected item index and spawns a Actor in world in front of the player (if any object is held in the inventory) after the Actor is spawned we remove the item from the inventory.
In the EventGraph of Character we implement the DropSelectedItem event (We previously mapped this in the Input settings) we simply call the DropItem function and update the selected item index so it’s valid in case we removed the last item of the inventory array.
The remaining two events implement the mouse scroll to move up and down the inventory. The index is clamped so it cannot go out of range of the total number of held items.
The HUD draws the items we currently hold in our inventory and the selected item is drawn slightly larger.
Add two new variables of type float InventorySpacingY and InventoryLocationY. Compile the blueprint and assign the following defaults:
The above blueprint will look like this, where the selected item is drawn slightly larger.
Create a new game mode or update your existing GameMode blueprint. Make sure it’s using BP_HUD.
Go to Window > World Settings and verify the correct gamemode is set for your level.
Post-Process for Item Highlighting
Select your primary post-process of your level (the default maps use a global post-process volume “Global PostProcess”) or create a new volume.
Make sure Unbound is checked so it affects the player regardless of position and assign the provided PP_OutlineCustomDepth material in the Blendables array.
Now you can try out your inventory system.
Creating Additional Items
If you want to create additional items such as a weapon you have to create child blueprints for both BP_InventoryItem & BP_PickupItem. Update the default values for both objects to your new blueprints and assign a new model.
A more advanced system could use DataTables as mentioned at the start of this tutorial.
- Make sure you’ve assigned all default values (DisplayName and InventoryClass / PickupClass variables)
- Check your Input mapping if all keys are assigned (“E”, “F” and scroll up/down)
- Check if correct game mode is used and has your HUD class assigned.
- If your objects are falling through the floor, make sure they have a collision primitive assigned.
How to use “Cast To”-nodes
Project source is available on GitHub!