In this section, you’ll create your first gameplay object: a Key Blueprint.
To create the functionality for gameplay objects in your level, you’ll use Blueprint Visual Scripting. Blueprints in Unreal Engine are assets with a physical representation and special functionality, like the player being able to pick them up.
The Blueprint Visual Scripting system can be used to create functionality without writing code, as you are using a node-based interface to create a gameplay object’s behavior.
First, you’ll define the key’s in-game actor, adding components that let you configure keys to appear in three different variations: a yellow oval, a blue rectangle, or a red cone. Instead of making separate assets for each variation, Blueprints let you build all of these options into one asset.
Next, you’ll add the key’s behavior — the ability for the player character to pick it up. Picking up objects is one of the most common gameplay mechanics.
Here's an overview of all of the assets you'll make to create a key with three different appearance options and allow a player character to pick up the key:
Before You Begin
Make sure you understand the following topics, covered in previous sections of Design a Puzzle Adventure:
Unreal Editor UI, such as the Content Browser and Viewport.
An understanding of Blueprints. See the Blueprint Foundations documentation for more information.
You’ll need the following assets from Project Setup and Level Blockout:
A level with blocked-out rooms.
Define Key Types and Key Attributes
Before you create the key blueprint, you’ll define some key data that the blueprint will use to create the red, yellow, and blue variations.
To represent the different types of keys used in your game, you’ll use both an Enumeration and a Structure.
| Asset Type | Description |
|---|---|
Enumerations | Also known as enums, store a list of unique values that you can use across your project. For the key, you will use an enum to define the different key types you can choose from (yellow oval, blue rectangle, red cone). |
Structures | Also known as structs, are used to group related information together. For the key, you will use a struct to hold all of the key’s attributes: a mesh (3D model), and material color (red, yellow, blue) to apply to that mesh. |
Each enumerator acts like a label on a container, and the struct is like the contents of each container.
Define Key Types in an Enum
First, create an Enumeration asset that defines your key variants: Red, Yellow, and Blue.
To create an enum asset, follow these steps:
Open the Content Browser, or a Content Drawer, by using the Content Drawer button in the bottom-left corner of Unreal Engine.
Create a place to store the blueprints and supporting assets you’ll create in this tutorial. Go to the Content > AdventureGame > Designer folder. Right-click in this folder and click New Folder.
Name the new folder
Blueprints.In the Blueprints folder, create another folder named
Core.Right-click in the Core folder, go to Blueprint, and click Enumeration from the Create Asset menu. Name the new asset
Enum_KeyType.Double-click the asset to open it in a new window.
The Enumeration contains a list of Enumerator options.
To add key types to the enum, follow these steps:
Click the Add Enumerator button three times to add three new enums to the list.
Change the first enumerator’s Display Name to
Blue. Change the second one toYellow, and the third one toRed.Save the enum asset file by clicking the Save button in the top-left corner of the window. Alternatively, use CTRL + S.
When you add an enumerator, you are adding an option to the list. Blue, Yellow, and Red are all values you’ll use to distinguish between the different key types.
In comparison to blueprints, you don’t have to compile data assets since you are not compiling any functionality.
Define Key Attributes in a Struct
Next, create a struct that defines the attributes that distinguish each key type: a mesh shape and a color.
To build a Structure (struct) asset, follow these steps:
Go to the Content Browser again and make sure that you are still in the Content > AdventureGame > Designer > Core folder.
Right-click anywhere in the Core folder, go to Blueprint, and click Structure. Name this asset
Struct_KeyData.Double-click the new asset to open it in a new window with the struct’s properties.
The struct will have a variable by default, named MemberVar_0. Click Add Variable to add a second variable.
Change the first variable’s name to
Key Materialand the second variable toKey Mesh.Each variable has a data type assigned to it. This is set to Boolean (a true or false value) by default. You need to change the type of both variables.
For the Key Material variable, click the dropdown that says Boolean. Enter
material instanceinto the search field, hover over Material Instance, and select Object Reference.Repeat these steps to change the variable type of Key Mesh to Static Mesh > Object Reference.
Save the struct asset file.
You have now created two attributes that each key type will use — a Key Material that will set the key color, and a Key Mesh that will set the key shape.
You’ve finished defining what sort of data the key has. Next, you’ll set up the key’s colors and then actually build each key type.
Create Key Colors
Now that you have the data for blue, yellow, and red key types, you’ll create Materials with corresponding colors that you’ll assign to each key type.
In Unreal Engine, a Material defines how a surface looks. It controls things like color, texture, shininess, transparency, and more. A Material Instance is a copy of a material that uses the material as a foundation, but can override specific settings like colors or textures, without needing to create a brand new material from scratch.
In this example, you will create a base Material that defines attributes for all keys in the game. Then, you will create two Material Instances of that material to copy all attributes but override the colors.
This section briefly guides you through creating some materials so you can use them throughout this tutorial series; Materials are not explained in detail.
To create a colored Material asset, follow these steps:
Go to the Content Browser. Navigate to the Content > AdventureGame > Designer folder.
Right-click in the Designer folder and click New Folder. Name this folder
Materials.Go to the Materials folder. Right-click in the folder and select Material to create a material asset.
Change the name of the material asset to
M_BasicColor.The tutorial’s sample level uses the
M_prefix as a naming convention to identify materials.Double-click the Material to open it. This opens the Material Editor in a new window.
The Material Editor is a node-based editor, meaning that it’s built with some similarities to Blueprint Visual Scripting. You should see a node called M_BasicColor, which is the main node that you can use to change the properties of this material.
The Base Color property sets the color of this material. You can click the gray color swatch next to Base Color to manually set the color.
Instead of using the color picker, you’ll create a Color parameter node that you can override in each material instance, therefore making one material that can become many colors. Creating a parameter is like adding a variable — it turns the material color into an editable parameter in a Material Instance.
To create a color parameter node, follow these steps:
Next to Base Color, you will see a pin. Click the pin and drag to an empty area of the graph.
In the dropdown list, type
VectorParameterinto the search field, and select VectorParameter.This will create a new node that has a color picker. A vector-type variable has three values so you can use it to define the RGB values of a color.
The node’s default name, Param, is highlighted when you first create the node. Rename the node Color to make it easier to identify in your Material Instances.
If Param isn’t highlighted, click the node, then click the node name to rename it.
On the new Color node, click the checkered square next to Default Value to open a color picker.
In the Hex sRGB field, change the value to
F7DE0000, a yellow color, and click OK.Save and close the Material asset.
Next, you’ll create Material Instances to add more colors without having to recreate the main material.
To create three Material Instances for your key colors, follow these steps:
In the Content Browser, right-click the
M_BasicColorasset you created. Near the top of the context menu, click Create Material Instance.Name the material instance asset
M_BasicColor_Red. Double-click the asset to open it.You’ll notice this window is different from the Material Editor. This is because a Material Instance does not need the full editing suite of a Material — instead, it focuses on modifying parameters you defined in the base Material.
In the Details panel, under the Parameter Groups at the top, expand Global Vector Parameter Values.
You will see a disabled Color, which is the VectorParameter node you added to the base material. Click the checkbox next to Color to enable and override it.
Click on the color swatch next to Color and set the Hex sRGB value to
F7005A00. This changes the material instance to a beautiful, hot-red color!It’s your turn to lead the way! Repeat this process to create two more Material Instances:
Create a Material Instance named
M_BasicColor_Blue. Override the Color with Hex sRGB =00AFF700and Save the asset.Create a Material Instance named
M_BasicColor_Yellow. Keep the base material color, and Save the asset.
Material Instance parameters inherited from a Material asset always use the parent material’s values unless you override them. For example, if you open the M_BasicColor material and change its color, the M_BaseColor_Yellow material instance would also change color; however, the Blue and Red material instances would keep their colors.
Build the Key Blueprint
Now that you’ve got all the pieces you need, it’s time to create the Key Blueprint. The Key Blueprint is the asset that you can add to a level to create an instanced actor of it, which has the functionality to be picked up by the player.
To create a new blueprint class for your key, follow these steps:
Go to the Content Browser and navigate to the Content > AdventureGame > Designer > Blueprints folder.
Create a new folder named
Key.Go to the Key folder. Right-click in the folder and select Blueprint Class.
Select Actor as the parent class.
Name this new actor
BP_Keyand double-click to open it.The tutorial’s sample level uses the
BP_prefix as a naming convention to identify blueprint classes.
The Actor parent class is commonly used for an object that can be placed in a level — it doesn’t come with additional features like movement, like in the Character parent class, but it can be used to build interactions and logic.
To dock a window in the main editor, drag its tab next to your level’s tab.
You need to add a few things to our Key blueprint to make it function properly:
A static mesh to visually represent the key in your project.
A collision volume that detects when the player is close enough to pick up the key.
A way to know what type of key it is.
Game logic to determine what happens when the player collides with the key.
Collision is the detection of two objects coming into contact at runtime. Box, capsule, and sphere collisions are shapes used to make these detections.
Set up the Key’s Components
First, set up the key’s components so you have something to place and interact with in the game world.
To add a collision component and mesh shape to the key, follow these steps:
In
BP_Key, go to the Viewport tab.In the Components panel in the top-left corner, click Add and search for and select Capsule Collision.
This is the collision volume that the player can touch to pick up the key. Without this, the player wouldn’t be able to detect the key.
You could add collision to the key mesh itself, but using a collision shape helps even out complex geometry, improving collision detection and game performance.
Select the Capsule component and click Add again. This time, search for and select a Sphere basic shape, and rename it
KeyMesh.By selecting the Capsule component and then adding the Sphere mesh, the mesh becomes a child of the Capsule, and the Capsule is the parent of the mesh. The child component inherits all properties from the parent.
Set up the KeyMesh component:
Select the KeyMesh component. In the Details panel, go to the Transform category. Next to Scale, change the values to
0.3,0.3, and1.0.This shrinks the sides of the mesh into an oval shape.
Under the Collision category, set Collision Presets to NoCollision.
You’ve added collision with the Capsule component, so this turns off collision on the mesh, ensuring the player character can smoothly collect the key without bouncing off the static mesh.
Set up the Capsule component:
Select the Capsule component. In the Details panel, under the Transform category, set the Location to
0.0,0.0,80.0.This makes the key float above the ground. The mesh moves with the capsule because the mesh is a child component that inherits all properties from its parent component.
Under the Shape category, set the Capsule Half Height to
60.0. This stretches the collision volume to better surround the mesh.
In the Components panel, select the DefaultSceneRoot. Click Add, and select Rotating Movement. This slowly spins your key to make it more interesting to the player.
Save and Compile the blueprint.
Set Up the Key’s Properties
Your key also needs to store information, like what type of key it is, and what materials and meshes correspond to each type of key.
To set this up, you’ll use the Key Type enum and Key Data struct that you created earlier.
To add a key type variable to the blueprint, follow these steps:
Under the Components panel, you will see a My Blueprint panel. In the Variables section, click the plus (+) button to add a new variable.
Name the variable
KeyType.By default, its type is Boolean. Click the type dropdown, search for Enum Key Type, and select it. This variable type is the enum that you created earlier — so it’s time to use it! Here, it means the KeyType variable can be set to Red, Yellow, or Blue.
In blueprints, the variable type is also called the pin type because variables appear as pins in nodes.
Click the Eye next to the KeyType variable to make it a public and editable variable. This is important to do so you can edit the value of this variable in the Unreal Editor later and set the type of each key in your level.
Click KeyType to select it. In the Details panel, next to the Category property, delete Default and enter
Setup.This makes the KeyType property appear in the Details panel under a category named Setup.
Each key type should have a shape and color combination. In the next step, you’ll create a Map variable to define these combinations.
To set up a variable that maps key types to shapes and colors, follow these steps:
In the My Blueprint panel, add a new variable named KeyMap, also of type Enum Key Type.
With the KeyMap variable selected, in the Details panel, next to the type dropdown, there’s another menu that sets the variable’s container type, or how many values the variable holds. Change the container type to Map.
When you set the Map container, another type dropdown appears next to the container type. This is the type of data stored in each map option. You defined this key data earlier in a struct, so click the dropdown and set the data type to Struct_KeyData.
Compile the blueprint so you can populate the KeyMap with materials and mesh shapes, setting up a material-mesh pair for each KeyType option.
In the Details panel, go to the Default Value category.
Next to Key Map, click the + button to add a new element to the map.
Change the Key Type from Blue to a different color. If you don’t do this, you might not be able to add a new element since an element with the default Key Type already exists.
Repeat this two more times to create second and third elements.
With your three map elements added, set up your key types with the following values:
Key Type Key Material Key Mesh Red
M_BasicColor_RedCone
Yellow
M_BasicColor_YellowSphere
Blue
M_BasicColor_BlueCube
Your KeyMap should now look like the following:
Save and Compile the blueprint.
Finally, you need a variable that stores a reference to the player character when they touch the key.
To create a variable that stores a reference to another actor, follow these steps:
Under the Variables section, add a new variable named
OtherActor.Change its type to Actor > Object Reference.
In the Details panel, change its Container Type to Single, since you only need to reference one actor who interacts with this key — the player.
Now, the key’s variables should look like the following:
Test the Key
Give the key a test and see what it looks like so far.
Go back to your level and, in the Content Browser, drag BP_Key into your level. You’ve created a blueprint that you can add instances of to your game, and that’s worth celebrating! However, it’s still gray.
With the key selected, go to the Details panel. Look for the Setup category you created, and find the Key Type property. You can change this from Blue, to Yellow, to Red, but it’s not changing your gray oval key yet.
This is because you’ve added components and data to your key, but those parts are not communicating with each other yet. This is where Blueprint Visual Scripting comes in — you’ll build logic (nodes and wires) to pass information around a blueprint and perform actions with that information.
Set Key Values With a Blueprint Function
Next, you’ll create a function that connects the KeyMap options to the KeyMesh component, automatically setting the correct mesh shape and material color for a key based on its type.
A function is a reusable set of blueprint nodes that performs a specific task. They help keep your node graphs organized and let you execute (or “call”) the same logic multiple times without recreating it. With functions, you can trigger the execution of that part of a blueprint's logic from other blueprints.
Instead of creating the function in the key blueprint, you’ll put it in a new Blueprint Function Library, which is a type of Blueprint asset that stores a collection of reusable functions. These functions are not tied to a specific blueprint or actor. Instead, they’re globally accessible across your entire project. Libraries keep useful functions in one place so you don’t have to copy and paste them into multiple blueprints.
To create a Blueprint Function Library with a new function, follow these steps:
Go to the Content Browser and navigate to Content > AdventureGame > Designer > Core.
Right-click in the Core folder, go to Blueprint, and select Blueprint Function Library. Name the new asset
BPL_FPGameand open it.In the My Blueprint panel, in the Functions section, you start with one new function that’s highlighted. Name it
fnBPLSetKey.The tutorial’s sample project uses a
BPLlabel for functions in a blueprint library and anfnprefix for functions. This helps identify what SetKey is and where it comes from, and makes it easier to find the function in the node actions list.
Next, set up the function with the inputs and outputs it needs.
Inputs are values you pass into a function so it can do its work. They act similarly to variables. The fnBPLSetKey function needs to perform actions between the KeyType, KeyMap, and KeyMesh, so these should be inputs.
Outputs are information that the function passes back to the blueprint, usually after creating a result. The fnBPLSetKey function doesn’t need any outputs.
To add inputs to the fnBPLSetKey function, follow these steps:
Click the fnBPLSetKey function to select it. In the Details panel, next to the Inputs category, click the + button to create a new input.
Like variables, each input has a name, variable pin type, and container type.
Change the input’s name to
Static Mesh Array.Change its type to a Static Mesh Component > Object Reference. In the function’s node graph, notice how the purple function entry node now has a pin with the input you created, and the style of pin reflects the type (static mesh array).
Click the Container Type dropdown next to the type, and choose Array.
An array is a variable that stores multiple values of the same type in a single container. Each value is stored as an array element.
Set up another input to match the key’s KeyMap property:
Add another input, and name it
KeyMap.Click the Pin Type dropdown and set it to Enum Key Type.
Click the Container Type dropdown and select Map.
Click the Value Type dropdown and set it to Struct Key Data.
Set up a Key Type input:
Add another input named
Key.Set its Container to Single, and keep its type as Enum Key Type.
Your three inputs should now look like this:
Now, you can build the function’s nodes. In the graph panel, you can see the function entry node fnBPLSetKey with the inputs you just created — a Static Mesh Array, Key Map, and Key.
Navigating the blueprint graph:
Pan: Right-click and drag.
Zoom: Use the mouse wheel.
To add logic to the function that sets up the keys with a new Key Mesh shape, follow these steps:
Drag off the triangle execution (exec) pin of this node, search for For Each Loop, and select it in the node actions list. This creates a new node connected to the fnBPLSetKey node.
With the For Each Loop node, you can perform some actions on each item in an array. Any logic you connect to the Loop Body execution pin runs once per array element. When the loop is done, execution continues through the Completed pin.
Drag the Static Mesh Array pin of the fnBPLSetKey node, and connect it to the Array pin of the For Each Loop.
The key only has one mesh, but fnBPLSetKey uses a static mesh array so you can use it with a variety of level objects. For example, in the next part of this tutorial series, you’ll work with the door blueprint that has two static meshes for the right and left doors.
Next, drag the Key Map pin of the fnBPLSetKey node and search for and select a Find node. This node can take a Key Type and pull the corresponding mesh and material from the KeyMap.
Right-click the blue output pin of the Find node, and click Split Struct Pin.
This splits the output into Key Material and Key Mesh, which are the two variables in
Struct_KeyData.Connect the Key pin of the fnBPLSetKey node to the turquoise Key pin of the Find node.
Now, on the For Each Loop node, drag from the Loop Body pin and search for Is Valid under the Struct category. Add the Is Valid node with a question mark icon (?) next to it, since this is the one that you use for struct values.
This node checks if a valid mesh shape was passed into the function. If it is, you’ll set both the static mesh of the object and its material. If not, you’ll only set the material.
Connect the Key Mesh pin of the Find node to the Input Object pin of the Is Valid node.
In the Is Valid node, drag the Is Valid pin and toggle the Context Sensitive option to false.
Search for and add a Set Static Mesh node.
From the For Each Loop node, drag the Array Element pin and connect it to the Set Static Mesh node’s Target pin.
The Target is the entity the node will act on. You want to execute the Set Static Mesh action on the KeyMesh, so the Static Mesh Array should be the target.
From the Find node, drag the Key Mesh pin and connect it to the Set Static Mesh node’s New Mesh pin.
The function’s graph should now look like this:
You can double-click a wire between two nodes to create an additional connector. Hover over the connector until your cursor turns into four arrows, and then drag the connector to reshape the wires. This can help you position your nodes and lines freely.
Select a wire and press the Q key to straighten the wire.
You’ve set the new mesh shape on the key, now you just need to set the new material as well.
To add logic that sets up the keys with a new Key Material color, follow these steps:
Drag the Set Static Mesh node’s Exec pin and search for the Set Material node (in the Rendering > Material category) and create it.
Drag its Target pin to the Array Element of the For Each Loop node.
Drag its Material pin to the Key Material pin of the Find node.
To make sure the material is set when a mesh isn’t provided, connect the Is Not Valid pin of the node to the Set Material node’s exec pin. Add two connectors to reshape the wire.
Save and Compile the blueprint.
Your complete fnBPLSetKey function should now look like this:
To copy this group of nodes into your project, click Copy Full Snippet, click a blank area in the corresponding graph in your blueprint, and press Ctrl + V. Then, connect the function entry node’s pins to the For Each Loop and Find nodes.
Initialize the Keys
You are now ready to use the functionality you’ve created inside the key blueprint. First, you will initialize the key based on the KeyType you assign to it.
To initialize a Blueprint level object means to set it up when it’s created, either in the viewport or when the game begins.
To initialize the key, follow these steps:
Drag the Exec pin of the Construction Script node and search for the fnBPLSetKey function you created earlier.
The function node requires a Static Mesh Array, Key Map, and Key, which you will provide from
BP_Key.In the My Blueprint panel, under the Variables section, expand the Setup category, and drag the KeyType variable into the node graph near the function node.
This prompts you if you would like to Get or Set the value of the variable. In this case, you need to check the type of key this is, so select Get.
Connect the new Get Key Type node’s pin to the Key pin of the Fn BPLSet Key node.
Repeat the same process for the KeyMap variable. Drag it into the node graph and create a Get node. Connect the pin of this node to the KeyMap pin of the Fn BPLSet Key node.
Next, from the Components panel, drag KeyMesh into the graph and create a Get node.
The Key Mesh node contains a single static mesh component reference, but the function node needs an array. Unreal Engine can take care of this for you: connect the Key Mesh pin to the Static Mesh Array pin of the Fn BPLSet Key node.
Save and Compile your blueprint.
The key’s construction script should now look like this:
If you copy this snippet into your project, you need to connect the FnBPLSetKey node to the Construction Script node.
Test the Key’s Functionality
Now, you can test the color-changing functionality you created in your key and new function.
Close down the Blueprint Editor window and go back to the level editor.
Since the default Key Type value is Blue, your key should be initialized as a blue rectangle.
Select the key actor, and in the Details panel, under the Setup section, change the Key Type between Yellow and Red. As you change it, you should see your key update its color and shape in the level!
Grant Keys to the Player Character
Your player needs a way to pick up keys, and for other objects to know what keys the player holds. To handle granting and retrieving keys, you’re going to use a Blueprint Interface.
Use a Blueprint Interface to Communicate Between Blueprints
A Blueprint Interface stores a list of functions that can be used by each blueprint that has access to the interface. To implement an interface means giving your blueprint the ability to respond to a shared set of events (or messages).
In a game where player and NPC characters are walking around, the key needs a way to make sure it’s only interacting with the player character. To do this, you'll create a Blueprint Interface to implement in BP_AdventureCharacter. This acts like a permission slip that the player blueprint uses to listen to the pick-up-key message.
Blueprint Interfaces are useful when you want different Blueprints to communicate without knowing each other’s details.
To create a Blueprint Interface with two functions, follow these steps:
Go to the Content Browser and navigate to Content > AdventureGame > Designer > Blueprints > Core.
Right-click in the Core folder, go to Blueprint, and create a new Blueprint Interface. Name this asset
BPI_PlayerKeysand open it by double-clicking on it.This window looks similar to the Blueprint Editor. On the right side, you have the My Blueprint panel that lists all functions in this interface. Below that is the Details panel.
In the My Blueprint panel, there’s already an initial function made for you. Name it
fnBPIAddKey.Select the function, and in the Details panel, under the Inputs section, press the + button to add a new input.
Name the input
KeyType, and change its type to Enum Key Type.Also in the Details panel, next to Category, enter
Keys. This makes your function easier to find in the blueprints’ Interfaces list.Next, add a new function in the My Blueprint panel named fnBPIGetKeys, which will handle retrieving keys.
Select the new fnBPIGetKeys function, and in the Details panel, under the Outputs section, click the + button to add a new parameter named Held Keys.
Change its type to Enum Key Type, and the return value to Array so it can return the player’s found keys.
Save and Compile your interface. You can now close the BPI_PlayerKeys window.
Since an interface is related to the blueprint system, you need to compile it just like you do with blueprints.
Notice that in the Blueprint Interface, you only created function declarations. Interface functions don’t contain logic themselves, but their setup determines how you can use them in blueprints.
Interface functions without outputs (fnBPIAddKeys) act like events, while functions with outputs (fnBPIGetKeys) work like regular functions you can add logic to.
Create a New Player Character
Before adding to the character, you’ll duplicate a default player character that comes with the project to create your own variant. You’ll also need to change your game mode settings to use your new character.
To create a player character to use in your project, follow these steps:
Go to the Content Browser and navigate to the Content > AdventureGame > Designer > Blueprints folder.
Create a new folder named
Characters.Go to the Content > Variant_Shooter > Blueprints > FirstPerson folder.
Drag the
BP_ShooterChracterasset into the Characters folder you created, and select Copy Here.Right-click the new copy in the Characters folder to rename it
BP_AdventureCharacter.Go to Edit > Project Settings. In the left panel, go to Project > Maps & Modes settings.
Under the DefaultGameMode setting, expand SelectedGameMode.
A Game Mode asset that defines a set of rules for the world — including the default player character. You will need to change it to use the new character you created. You can change game mode settings here, or by opening the asset itself.
Change the Default Pawn Class to
BP_AdventureCharacter.Close the Project Settings window.
Add the Blueprint Interface to the Player
In this section, you’re going to modify the player character blueprint. This character has many helpful functions and interfaces already set up for you, and you’ll add the BPI_PlayerKeys interface you made earlier to allow them to pick up keys.
Next, you will modify the new BP_AdventureCharacter to use keys.
To add the Blueprint Interface functions to the character, follow these steps:
Navigate to your new Characters folder and open
BP_AdventureCharacter.You’ll add the BPI Player Keys interface to make sure that the player blueprint implements it. In the toolbar at the top of the new window, click the Class Settings button.
In the Details panel on the right-hand side, under the Interfaces > Implemented Interfaces section, click Add, then select
BPI_PlayerKeysfrom the dropdown. This grants your character the ability to implement the fnBPIGetKeys and fnBPIAddKey functions.Compile the blueprint so you can use the interface functions. In the Interfaces section of the My Blueprint panel, you’ll see a Keys category containing fnBPIAddKey and fnBPIGetKey. It has a yellow icon instead of gray, meaning these functions act like events.
Store Picked Up Keys
The player needs to store the keys they’ve collected, so create a new variable for this. The variable needs to hold up to three keys, so it should be an array.
To create an array variable to store the player’s keys, follow these steps:
In the
BP_AdventureCharacterblueprint, add a new variable to the Variables list in the My Blueprint panel.Name the variable
Held Keys.Select the variable, and in the Details panel, set the container to an array of the type Enum Key Type. Using an array is important since the player can hold multiple keys simultaneously.
To add keys to the Held Keys array when the player picks up a key, follow these steps:
Go to the character’s Event Graph tab.
Right-click the graph and search for Event fnBPIAddKey. This is the event node that executes when the fnBPIAddKey function is called.
Drag from its execution pin and search for
Array Add, and select an Add node. This node takes a new element and adds it to an array.Add a reference to the Held Keys variable by dragging the variable into the Event Graph, and select Get.
Connect the Held Keys node to the Array Add node’s Target Array input. This pin looks like a square.
Connect the Event node’s Key Type pin to the Add node’s New Item input.
Now, when this event function is triggered, the player character adds the provided Key Type to their Held Keys array.
Retrieve the Player’s List of Keys
The door you’ll work with in the next part of this tutorial needs to check all of the player’s keys, so add functionality to the player that returns an array of keys they have picked up.
To handle retrieving keys, follow these steps:
In the
BP_AdventureCharacterblueprint, under My Blueprint > Interfaces, expand the Keys section.In the list, fnBPIGetKeys has a grey icon, meaning you can use it as a regular function, not just an event.
Double-click fnBPIGetKeys to open the function’s graph.
Add a reference to the Held Keys variable by dragging it in and selecting Get.
Connect the Get Held Keys node to the Return node’s Held Keys pin.
The fnBPIGetKeys graph should now look like the following:
Make Keys Detect Collisions With the Player
Next, you will add functionality that triggers the fnBPIGetKeys event function when the player picks up a key. This should happen when the player touches the key’s collision volume.
You will add an event that belongs to the Capsule collision component. An event triggers when something happens during gameplay. In this case, when the player actor overlaps with the key’s Capsule, the key should perform some actions.
To add an overlap event and check if the overlapping actor is the player, follow these steps:
Open your
BP_Keyblueprint and navigate to an empty area of the EventGraph.In the Components panel, right-click the Capsule component and select Add Event > Add OnComponentBeginOverlap. The associated node appears in your event graph. This event node triggers if an overlap happens between this and another actor.
The event node comes with information about that event. For example, the overlapping actor that triggered the event is passed through the Other Actor pin.
Drag your OtherActor variable from the Variables list into your graph and select Set. A Set node saves a new value to that variable.
Drag the Exec pin of the Event OnComponentBeginOverlap node and connect it to the Exec input of the Set Other Actor node.
Drag the Other Actor pin of the Event OnComponentBeginOverlap node and connect it to the Other Actor input of the Set Other Actor node.
Before you do anything with the key, check if the overlapping actor is the player, since that’s the only character that should be able to collect keys. You know it’s the player if they have the Blueprint Interface.
Right-click near the Set node and add a Does Object Implement Interface node.
On the Does Object Implement Interface node, change the Interface pin to BPI_PlayerKeys.
Connect the Test Object input pin to the Set Other Actor node’s blue pin. This pin works like a Get Other Actor node.
On the Does Object Implement Interface node, drag the Return Value pin and add a Branch node.
Connect the exec input pin of the Branch node to the Set node.
A Branch node checks if a condition is true or false so you can execute different actions for each result. Here, you’ll create logic for two scenarios, true and false, based on what happens if the overlapping actor is or isn’t the player.
To call the add-key function when the player has touched the key, follow these steps:
If the Branch result is true, the actor can pick up keys, so trigger the interface’s event function. Drag off the True pin of the Branch node and add an Fn BPIAdd Key (Message) node.
You’re adding a message node instead of a regular function node because when you call a blueprint interface function, you’re sending a message. The Target blueprint only responds if it implements that interface.
Drag the Fn BPIAdd Key node’s Target pin and add a Get Other Actor node.
Drag the Key Type pin and add a Get Key Type node. This is another way to add variable references.
When your key is picked up, it should disappear after a short delay so it can’t be picked up again. To handle this, drag the Fn BPIAdd Key node’s exec pin and add a Delay node. Keep the default Duration of
0.2.When the timer expires your key should disappear, so connect a Destroy Actor node to the Delay node’s Completed pin. Make sure the Target is set to self, meaning that the key actor should remove itself from the level.
Save and Compile your blueprint.
You’ll notice you didn’t add any nodes to the Branch node’s False result. This is because if the overlapping actor doesn’t have the keys interface, they can’t pick up the key, so do nothing.
The key’s complete event graph should now look like the following:
Here's the asset diagram from the beginning of this page again, but this time it shows a summary of all the assets and functions you've created and how they combine together to create the key's appearance and behavior:
Test the Final Key
Go back to your level and add a key of each color. You can now try to pick up the keys in-game by walking over them.
To place a key on the floor after dragging it in from the Content Browser, press End. This snaps the selected level object to the floor.
Next Up
In the next section, you’ll change the door blueprint to be able to change color like the keys and remain closed unless the player has picked up the corresponding key.
Open Doors with Keys
Configure the BP_DoorFrame blueprint so the doors can change color and only open with the matching BP_Key.