InventoryAdding Items during Gameplay
This how-to will show you how to add new items to the inventory during gameplay, which is a common task for things like pickups, quest rewards, and more.
In your Content Browser, navigate to your item data folder. If you are creating a new folder, it should match the configuration in your Asset Manager.
Right-click in any empty area, select the Inventory category, and then Item Definition.
Name your new container asset
Consumable_HealthPotion
and open it.In the Gameplay Tags property, add the
Inventory.Item.Potion.Health
Gameplay Tag to identify this item.Add the User Interface Fragment to configure how this item is presented in the user interface. Set appropriate values for it.
Add the Stack Fragment. Set a
Stack Limit
of10
and a Maximum Limit of100
. Set it to Automatically Merge New Items.Add the Container Fragment and configure your Preferred Container Query to prioritize the backpack container.
When you are done, your Health Potion should look like this (except maybe for the User Interface details).
Create an
Add Health Potion
event to retrieve the Inventory Manager and add the new Health Potion with a default stack of5
.BlueprintC++In your Character, declare a Custom Event named
Add Health Potion
.In your Character, declare a Blueprint Callable Function named
Add Health Potion
.public: UFUNCTION(BlueprintCallable, Category = "Inventory") void AddHealthPotion();
void UInventoryExamples::AddHealthPotion() { TArray<FInventoryDefaultItemMemory> Memories; const int32 StackSize = 5; const FInventoryDefaultItemMemory StackMemory = UNinjaInventoryGameplayFunctionLibrary::CreateStackMemory(StackSize); Memories.Add(StackMemory); FInventoryItemContext ItemContext, ResultContext; ItemContext = UNinjaInventoryFunctionLibrary::CreateItemContext(PotionItemData, Memories); UNinjaInventoryManagerComponent* InventoryManager = GetInventoryManager(); InventoryManager->AddItem(ItemContext, ResultContext); if (ResultContext.IsSuccessful()) { UE_LOG(LogTemp, Log, TEXT("Potion added!")); } }
In your Character, create a Blueprint event from
Debug Key K
. Drag from the execution pin and callAdd Health Potion
.tip
Normally, the item would be added from other Gameplay Systems, but using a Debug Key is a quick way to test features.