In this tutorial, you’ll learn to create a blueprint that spawns new cubes in the level when the player stands on top of a switch. Use this gameplay object to allow players to get new cubes in any puzzle room where cubes can be lost or destroyed.
For the cube spawner, you’ll create a blueprint that only contains the spawning functionality, so you have the flexibility to use any piece of geometry or art asset to create the effect of cubes falling out of that geometry.
Creating this blueprint isn’t mandatory if you’ve followed along with the sample level, but you may want to include this cube-regeneration mechanic in your project if you continue expanding the puzzle adventure game.
Before You Begin
Make sure you understand these topics covered in the previous sections of the Design a Puzzle Adventure tutorial series:
Blueprint basics, including Blueprint Interfaces, custom events, and variables.
You’ll need the following assets created in Puzzles: Switches and Cubes:
BP_CubeBlueprintBP_SwitchBlueprintBPI_InteractionBlueprint Interface
Create a New Blueprint
First, create a new blueprint asset for the cube spawner. This blueprint will implement the BPI_Interaction Blueprint Interface so it can respond to BP_Switch, which uses the events in that interface.
To set up a new blueprint, follow these steps:
In the Content Browser, go to the Content > Adventure Game > Designer > Blueprints > Activation folder.
Right-click and select Blueprint Class. In the class-selection window, select the Actor class.
Name this blueprint
BP_CubeSpawnand open the blueprint.In the Blueprint Editor, go to the EventGraph tab.
At the top portion of the blueprint editor, click the Class Settings button.
In the Details panel, under the Interfaces section, click the Add dropdown next to Implemented Interfaces and select BPI Interaction.
Click Compile and Save.
After adding the BPI_Interaction interface, under the My Blueprint panel, you’ll see a new section named Interfaces. This section contains the fnBPISwitchOff and fnBPISwitchOn events that are part of the BPI Interaction interface.
In BP_CubeSpawn, you’ll use the fnBPISwitchOn event to define what happens when the player steps on a switch and triggers this event.
Build the Cube-Spawning Logic
Before implementing the logic to spawn the cubes, consider how the cube spawning system should work:
To spawn a cube, the player must activate the switch, which uses the interface you added in the previous section.
There should be a maximum number of cubes the player can spawn so they don’t overfill the area with cubes.
If the player tries to spawn more cubes than the maximum amount, destroy the first spawned cube. You’ll keep track of the spawned cubes with an array variable.
The cube spawner should use a short cooldown timer so the player can not spawn new cubes too quickly.
Check Cube-Spawning Conditions
To start the cube spawner’s event graph, first check if a new cube can spawn and if the player has created the maximum number of cubes.
To check if a new cube can spawn, follow these steps:
In the My Blueprint panel, under the Interfaces category, double-click the fnBPISwitchOn interface to add it to the graph.
From the fnBPISwitchOn node, drag the exec pin and add a Branch node.
On the Branch node, drag the Condition pin, select Promote to variable, and name this variable
CanSpawn. You’ll use this variable to check if the cooldown has passed or not.In the My Blueprint panel, create a new variable named
CubesSpawnedof type Integer. This keeps track of the spawned cubes in the level.Create another variable named
MaxNumCubes. This variable sets the maximum number of cubes that the player can spawn.Set up MaxNumCubes:
Set its type to Integer.
Click its eye icon to make it public and editable.
In the Details panel, click the Category field and enter
Setup.Click Compile and set the variable’s Default Value to
3.So far, your variables list should look like this:
From the Branch node, drag off the True pin and add a second Branch node.
From the second Branch node, drag the Condition pin and add a Less Than (<) node.
Set up the Less Than node to check if the number of cubes spawned is less than the max number of cubes allowed:
From the Variables list, drag CubesSpawned onto the Less Than node’s top input pin.
Drag the MaxNumCubes variable onto the bottom input pin.
Count Spawned Cubes and Destroy Extra Cubes
If the current number of cubes the player has spawned is less than the maximum, execution passes through the second Branch node's True pin. In this case, you can skip directly to the logic that adds 1 to the cube count before spawning a cube.
To increment the number of spawned cubes, follow these steps:
From the second Branch node, drag the True pin and add an Increment Int node. Move the new node to add extra space between it and the Branch node.
From the Variables list, drag CubesSpawned onto the Increment Int node’s input pin.
Next, add the logic for the opposite scenario — where the Cubes Spawned value is greater than the Max Num Cubes value, or when execution passes through the Branch's False pin. If the player has reached the maximum number of cubes, destroy the oldest cube before spawning a new one.
To destroy a cube to make room for a new cube, follow these steps:
From the second Branch node, drag the False pin and add a Destroy Actor node.
From the Destroy Actor node, drag the Target pin and add a Get (a copy) node.
Keep the Get node’s second input pin set to 0, since an array’s list begins at 0 and you want to retrieve the first element.
From the Get node, drag off the Array input node and select Promote to variable.
Name the new variable
SpawnedCubes. You’ll use this array to store every cube the player spawns into the level.From the Destroy Actor node, drag the exec pin and add a Remove Index node.
Drag its Array input and add a Get Spawned Cubes reference node.
Double-click the wire between the second Branch node and the Increment Int node to create a movable reroute node. Drag the reroute node closer to the Increment Int node.
Connect the Remove Index node’s output exec pin to the reroute node.
(You can also connect the Remove Index node directly to the Increment Int node.)
Now, before a cube spawns, the blueprint either increments the CubesSpawned counter or deletes a cube and then increments the CubesSpawned counter. Next, you’ll add the logic that spawns a new cube.
Spawn a Cube
Now that you’ve checked if the player can create a cube and ensured there aren’t too many cubes in the level, you can spawn a new BP_Cube actor.
To spawn a new cube, follow these steps:
From the Add node, drag the exec pin and add a Spawn Actor from Class node.
Set up the SpawnActor node:
Click the dropdown menu next to the Class pin, search for, and then select
BP_Cube. The node’s name changes from SpawnActor NONE to SpawnActor BP Cube.Right-click on the orange Spawn Transform pin and select Split Struct Pin. Three new pins replace the Spawn Transform pin: Spawn Transform Location, Spawn Transform Rotation, and Spawn Transform Scale.
You have to right-click the circular Spawn Transform pin specifically, not the entire node.
Right click in the Event Graph and search for then select a Get Actor Transform node. Right click on the Return Value pin and select Split Struct. Connect the Location and Rotation pins to the corresponding pins in the Spawn Actor node.
From the SpawnActor node, drag the exec pin and add an Add node under the Array category (you can also search for
Add Arrayand select the Add node).On the Add Array node, do the following:
Drag the Array input pin and add a Get Spawned Cubes node.
Connect the node's bottom input pin to the SpawnActor node’s Return Value pin.
When the player spawns a cube, they shouldn’t be able to spawn one again until a cooldown timer expires. Drag the Add Array node’s exec pin and add a Set Can Spawn node. Keep Can Spawn set to false (does not have a checkmark).
Implement a Cooldown Timer
Use the CanSpawn variable and a Delay node to prevent the player from spawning another cube until a set amount of time has passed.
To create and trigger an event that resets the cube-spawn cooldown timer, follow these steps:
Right-click somewhere empty in the EventGraph, and search for and select Add Custom Event.
Name the new event
EvResetSpawnand move it under the Event fnBPISwirchOn node.Drag the EvResetSpawn event node’s exec pin and add a Delay node.
On the Delay node, drag the Duration pin and select Promote to variable. Name the variable
SpawnCooldown.Set up the new SpawnCooldown variable:
Ensure its type is Float.
Click its eye icon to make it public and editable.
Change its Category to Setup.
Click Compile and then set its default value to
1. Your complete list of variables should look like this:
From the Delay node, drag the Completed pin and add a Set Can Spawn node. Make sure to toggle this node’s Can Spawn value to true.
Return to the Set Can Spawn node at the end of your cube-spawning logic. Drag the Set Can Spawn node’s exec pin and add an EvResetSpawn node to trigger the event and spawn cooldown delay.
Click Save and Compile.
Now, your EventGraph should look like this:
If you use the snippet below, ensure your blueprint has the custom variables, event, and BP interface mentioned in the instructions above.
To copy these nodes into your blueprint, click Copy Snippet, go to your event graph, and press Ctrl+V. After copying the logic into your blueprint editor, you may need to delete the blue Ev Reset Spawn node and re-add it manually after the Set Can Spawn node.
You can now exit the blueprint editor and return to the level editor.
Add the Cube Spawn Blueprint to the Level
Next, you’ll set up a BP_CubeSpawn and a BP_Switch to the level.
The cube-spawner blueprint has no visual components, so you can place it within any geometry or art assets to create the effect of a cube falling out of that geometry.
Pick a location in your level and use the meshes in the Content > LevelPrototyping > Meshes folder to block out some geometry to represent the cube spawner.
If you’re using the sample level, you can place it in the space at the back of Room 1. For this example, we’ll use a cylinder to represent a pipe that the cubes can spawn inside and fall out of.
Then, to set up a BP_CubeSpawn in a level, follow these steps:
In the Content Browser, navigate to the Content > Adventure Game > Designer > Blueprints > Activation folder.
Drag the
BP_CubeSpawnblueprint into a desired location in the level.From the Content Browser, drag the
BP_Switchblueprint into the level within line of sight of the cube spawner.Select the
BP_Switchactor in the level.In the Details panel, under the Setup section, add a new array element to the Interact Object List.
Use the dropdown next to Index [0] to select the
BP_CubeSpawnactor. This passes the fnBPISwitchOn event from this switch to theBP_CubeSpawnactor.Select the
BP_CubeSpawnactor. Optionally change the Max Cubes Allowed variable to the number of cubes you want to allow in the level.
Now, if you play the game, you can step on the switch to spawn new cubes.
Next Up
To continue working on your level by adding materials, lighting, post-process effects, sound, and visual effects, try the Artist Track Tutorial Series:
Art Pass for a Puzzle Adventure Game
Learn how you can apply art workflows with materials, sounds, and visual effects to the Puzzle Adventure game.
If you are interested in packaging your project, as a standalone program to test and share, see the following documentation:
Packaging Unreal Engine Projects
Packaging Unreal game projects for distribution.