How-to: a pickup that adds to an inventory

Build a pickup that, when the player walks into it, adds its item name to the player's inventory array and removes itself.

Last updated Oct 26, 2026Edit this pageVersion 1.0.0

This guide builds two classes that talk to each other: a pickup that, when the player walks into it, hands its item name to the player and removes itself, and a player that keeps the names in an array. You will use an array variable, the overlap payload, Cast To, and Call On Target into another class's function.

Build the door first if you have not used a Trigger slot yet.

  1. Player enters pickup trigger
  2. BP_Pickup: OnTriggerEnter
  3. Cast To BP_Player
  4. Call On Target: AddItem
  5. Inventory grows, pickup removed
What happens on pickup

Part 1: the player's inventory

  1. Create the player class

    In the Content Browser, choose Visual Script Class > Character and name it BP_Player. Use it as your game mode's player pawn.

  2. Add the inventory variable

    In Variables, add Inventory of type String.

  3. Add AddItem

    Click + in Functions, name the function AddItem and give it one String input, item.

  4. Write the body

    Wire the entry node into an Array Add node, set its variable to Inventory, and feed the entry node's item pin into its value. The first Array Add turns the String variable into an empty array and appends to it.

  5. Compile and save

    Click Compile, then Save.

Tip

The pickup calls one function instead of writing the player's variables. If the player later caps the inventory or plays a sound, that change lives in AddItem and no pickup changes.

Part 2: the pickup

  1. Create the class

    Choose Visual Script Class > Actor and name it BP_Pickup.

  2. Add variables

    Add ItemName (String, default Coin, Instance Editable on) and Collected (Bool, default false). Placed pickups can then grant a coin, a key or a potion from the same class.

  3. Add components

    Add a MeshRenderer component so the pickup is visible, and a Trigger component with shape Sphere and a radius that covers it.

  4. Handle OnTriggerEnter

    Add an OnTriggerEnter event graph and wire:

    1. Event into a Branch on Get Variable Collected. Continue from False only.
    2. Cast To BP_Player, with Get Variable PinPayload as the object. PinPayload holds the actor that entered.
    3. From the cast's success output, Call On Target with the function AddItem. Wire the cast's object output into Target and Get Variable ItemName into the argument.
    4. Set Variable Collected to true.
    5. DestroyActor from the Gameplay Library, with Self as the actor MGameplayStatics.
  5. Compile and save

    Compile, then save.

Call On Target finds no reflected AddItem on the player's class, so it runs the player's visual function in the player's context, and the string lands in the player's Inventory. If something other than the player brushes the trigger, the cast fails and nothing happens.

Visual Scripting editor with BP_Pickup open on the OnTriggerEnter graph: Event OnTriggerEnter into a Branch on Collected, the False output into Cast To BP_Player fed by Get PinPayload, the cast's success output into Call On Target AddItem with Get ItemName as the argument, then Set Collected true and DestroyActor with Self
The pickup's OnTriggerEnter graph.

Part 3: place and play

Drop several BP_Pickup instances in the level and set each one's ItemName in its Details panel. Start Play-In-Editor and walk over them.

To watch it, open BP_Player while playing and put a breakpoint on the Array Add node in AddItem. Each pickup pauses there, and the watch strip shows Inventory growing. A logpoint with the message got {item} prints each pickup without stopping. See Debugging.

Take it further

  • No duplicates. In AddItem, check Array Contains on Inventory and Branch before adding.
  • Counts instead of names. Use a map of item name to count: read the current count with Map Find and write it back with Map Add.
  • Tell the HUD. Give BP_Player an OnInventoryChanged event dispatcher and Broadcast it from AddItem, so a listener updates without polling. See Functions, macros and dispatchers.

Engine sources

This page describes behavior implemented in these files of the engine repository, checked against commit 5922e6e5e.

  • Engine/Source/Runtime/Game/Public/Game/MGameplayStatics.h

Page feedback

Was this page helpful?