Create the Inventory UI
Let's create the first part of the Inventory UI, so it can display the weapon slots and the backpack. These will be built using the base Inventory Widgets and UMG Viewmodels.
Main Interface Elements
First, we need to create all main widgets that represent our current Inventory Layout: Primary and Secondary weapons, the backpack for all other items, and the Inventory Window itself.
Create the Item Widget
Create a new Widget Blueprint based on
NinjaInventoryItemWidget
.Design the widget, making sure that you have a valid Image Component for the icon and a Text Component for the quantity. Mark these component as a variables.
Go to the Graph and, in the
PreConstruct
event, hiding components that are not shown by default, marking them as collapsed.Create a new Blueprint Function,
SetIcon
, to receive the Icon and set the texture to the correct component. This function will receive aTexture2D
object.Create a new Blueprint Function,
SetStack
, to receive the Quantity and set the value to the correct component. This function will receive anInteger
value.Back to the Widget Designer, add the Item User Interface and Item Stack viewmodels. Configure their Creation Type to Create Instance.
Add two View Bindings, mapping the
Icon
property from the User Interface Viewmodel to theSetIcon
function, and Stack Size property from the Item Stack Viewmodel to theSetStack
function.
Create the widget for the primary weapon
Create a new Widget Blueprint based on
NinjaInventoryContainerWidget
.Click the root element in the widget and set the Container Data to the Primary Container and set the Item Widget Class as the Item Widget created in the previous step.
Design the widget as you like, making sure that you have a valid Named Slot labeled
ContainerViewSlot
. In this Named Slot, add an Inventory Container Border, and finally, inside the Container Border, add the Item Widget created in the previous step.Add the Inventory Items View Model, set it to be instantiated when the widget is created, and bind it to the
RefreshItems
function.
Create the widget for the secondary weapon
Create a new Widget Blueprint based on
NinjaInventoryContainerWidget
.Repeat the same tasks from the previous step, but set the Container Data to the Secondary Container.
Create the Backpack Widget
Create a new Widget Blueprint based on
NinjaInventoryContainerWidget
.Click the root element in the widget and set the Container Data to the Backpack Container and set the Item Widget Class as the Item Widget created in the previous step.
Design the widget as you like, making sure that you have a valid Named Slot labeled
ContainerViewSlot
. In this Named Slot, add an Inventory Container Box. Enable Fill Empty Slots. Depending on your design, you might want to also set Wrap Size and Explicit Wrap Size.Add the Inventory Items View Model, set it to be instantiated when the widget is created, and bind it to the
RefreshItems
function.
Create the Inventory Window Widget
Create a new Widget Blueprint based on
NinjaInventoryWindowWidget
.Design the widget as you like, making sure that you incorporate all widgets previously created: Primary Weapon, Secondary Weapon and Backpack. Also add a Text Component for the title that will be obtained from the Layout Data Asset.
Add the Inventory Layout View Model, set it to be instantiated when the widget is created, and bind the Display Name property to the
Title Text
.In the Blueprint Graph, create a Show Inventory event that will set the Input Mode to Menu and show the mouse cursor. For now, this will always be called during the Construct event. The Hide Inventory function reverts that setting.
Update the Gameplay Widget
Open the Gameplay Widget previously created for the Player Attributes and add the Inventory Widget, organizing it whenever you see fit.
Inventory Window Visibility
Configure Input Actions and Input Handlers
Create an Input Action for the Inventory Window and add a Pressed trigger to it.
Add the Input Action to the Input Mapping Context, mapping it to appropriate keys such as I and Gamepad Special Right.
Add a new entry to the Input Setup, with a HUD: Send UI Event handler. Set the correct Input Action and the
UI.Action.Inventory
Gameplay Tag.
Handle interface events in the HUD
Add
HUDEventDispatcherInterface
to your Gameplay HUD.Convert your Gameplay Widget instance, in the Gameplay HUD, to a variable.
Implement the
HandleInputEvent
function, so it invokes the same function (interface message call) in the Gameplay Widget Instance.
Handle interface events in the Widget
Add
HUDEventDispatcherInterface
to your Gameplay Widget.In your Construct event, set the Inventory Window visibility to be collapsed.
Create a new function,
ToggleInventoryWindow
that will adjust the Inventory Window visibility and set the correct Input Mode.Implement the
HandleInputEvent
function from the new interface, routing the Inventory Event to the new function.Open your Inventory Window Widget and remove all visibility-related features created in this guide.
Test everything
Press Play and your Inventory Window should be hidden.
Press your Inventory Key and your Inventory Window should appear. Your mouse cursor should appear as well.
Press your Inventory Key again and your Inventory Window should disappear, along with the mouse cursor.
Drag and Drop and Tooltips
With the main Inventory UI being shown, it's time to add special features, such as dragging stored items to perform operations and adding a tooltip for additional item details.