Ninja Bear Studio Plugins Help

Adding 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.

Create a Health Potion definition

  1. 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.

  2. Right-click in any empty area, select the Inventory category, and then Item Definition.

  3. Name your new container asset Consumable_HealthPotion and open it.

  4. In the Gameplay Tags property, add the Inventory.Item.Potion.Health Gameplay Tag to identify this item.

  5. Add the User Interface Fragment to configure how this item is presented in the user interface. Set appropriate values for it.

  6. Add the Stack Fragment. Set a Stack Limit of 10 and a Maximum Limit of 100. Set it to Automatically Merge New Items.

  7. Add the Container Fragment and configure your Preferred Container Query to prioritize the backpack container.

    Iron Bar Preferred Container Query
  8. When you are done, your Health Potion should look like this (except maybe for the User Interface details).

    Health Potion Item Data Asset

Add the Health Potion via Gameplay

  1. Create an Add Health Potion event to retrieve the Inventory Manager and add the new Health Potion with a default stack of 5.

    In your Character, declare a Custom Event named Add Health Potion.

    Adding Health Potions

    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!")); } }
  2. In your Character, create a Blueprint event from Debug Key K. Drag from the execution pin and call Add Health Potion.

    Adding Health Potions

Check your Inventory in the Gameplay Debugger

  1. Hit the Play button and press the K key to add the potion.

  2. Open the Gameplay Debugger to confirm that the Health Potion has been added to the inventory.

    Health Potion added to the Inventory
Last modified: 12 August 2024