Spawning/Destroying an Actor Overview

A How To Guide for Spawning Actors in Unreal Engine 4.

Windows
MacOS
Linux
On this page

Often times in your game you will find that you want to add items to your levels dynamically during gameplay, this is called Spawning an Actor. Items you want to spawn could be anything from other players or enemies to pick-ups such as Health, Weapons, or Rewards. On this page, you will find information regarding how to spawn an Actor through Blueprints.

Implementation Guide

Regardless of the type of game you are making, more often than not there are going to be instances where you want something to appear at runtime that was not originally placed in the level. The concept of spawning an object in the world can be achieved with Blueprint Visual Scripting as well as the ability to destroy or despawn objects, whether or not they were spawned in during gameplay.

In this guide, we will spawn in an Actor with the press of a button, attach it to a character, then destroy it when the button is pressed a second time using a variable reference.

HeroImage.png

Steps

For this how-to guide, we are using the Blueprint Third Person Template with Starter Content enabled.

  1. Inside your project on the Main Toolbar, click the Blueprints button then select Open Level Blueprint.

    SpawnDestroy_01.png

    The Level Blueprint is a specialized type of Blueprint that is unique to each level and acts as a level-wide global graph.

  2. Right-click in the graph. Search for and add the Spawn Actor from Class node.

    SpawnDestroy_02.png

    This will enable us to define the thing that we want to spawn by specifying the class (or type of object) we want to spawn.

  3. From the SpawnActor node Class pin, select the Blueprint_Effect_Fire class.

    SpawnDestroy_03.png

    This is a Blueprint Class that was created as part of the Starter Content pack enabled when creating the project.

  4. Right-click in the graph. Search for and add the F Keyboard Event.

    SpawnDestroy_04.png

    This will be the key that we will use to spawn and destroy our fire effect.

  5. Right-click in the graph again. Search for and add the Flip Flop node.

    SpawnDestroy_05.png

    This node acts as a switch. The first time we enter it, we will execute the A pin. The second time we enter this node, it will execute the B pin. Once the B pin executes, the next time we enter the node, we will execute the A pin again and flip-flop back and forth between the A and B pins.

  6. Click and drag off the F node Pressed pin and connect it to the Flip Flop and Spawn Actor nodes.

    SpawnDestroy_06.png

    This will enable us to execute (when F is pressed) the call to the Spawn Actor node to spawn in our Blueprint Fire effect.

  7. Right-click in the graph. Search for and add the Destroy Actor node.

    SpawnDestroy_07.png

    With this node, we can destroy any Actor we specify.

  8. Connect the Flip Flop node B pin to the Destroy Actor node.

    SpawnDestroy_08.png

    The second time we press F, the Flip Flop node B pin will be called and will execute the Destroy Actor node. The Destroy Actor has a Target input pin, instructing the node what it is to destroy. By default, it is set to Self and in this case is a null pointer, meaning it is unclear what it is to destroy as the Level Blueprint cannot be destroyed. If this Destroy Actor node were sitting inside our Character Blueprint and we instructed it to target Self, then our Character would be destroyed.

    In this case, we need to tell the Destroy Actor node what it should be destroying (our Blueprint Fire effect). To do this we will need to create a reference to the Blueprint Fire effect that we spawn.

  9. Right-click on the Spawn Actor node Return Value pin and select Promote to Variable.

    SpawnDestroy_09.png

    The Return Value, in this case, is what we spawn and what we want to store as a reference so that we can destroy it later.

  10. In the Details panel for the variable, give it a name such as CurrentFlames.

    SpawnDestroy_10.png

  11. In the MyBlueprint panel under Variables, Ctrl-drag the CurrentFlames variable and drop it into the graph. Connect it to the Destroy Actor node Target pin.

    SpawnDestroy_11.png

    We are now instructing the Destroy Actor node to target our variable reference to the Blueprint Fire effect we created.

  12. Right-click in the graph. Search for and add the Get Player Character node.

    SpawnDestroy_12.png

    This enables us to locate the designated player character so that we can access and obtain information about the character.

  13. Drag off the Get Player Character node Return Value pin. Search for and add the Get Actor Transform node.

    SpawnDestroy_13.png

    This will tell us the Location, Rotation and Scale of our player character and we will use this to define the spawn location of our Blueprint Fire effect.

  14. Connect the Get Actor Transform node Return Value pin to the Spawn Actor node Spawn Transform pin.

    Click image for full view.

    Our graph at this point is almost complete. We designated that pressing the F key will spawn in our Blueprint Fire effect at the location of our player and store it as a variable. When we press F a second time, we will then destroy the variable we created (killing off the fire effect). The last thing that we will do is attach the fire effect to our player. Currently, when we spawn in our fire effect it will spawn at the location of our player. However, if the player moves around, the fire will stay in the location in which it was spawned.

  15. Drag off the Set node reference pin. Search for and add the AttachToActor node.

    SpawnDestroy_15.png

    With this node, we can attach the fire effect to another Actor. In this case, we will want to attach it to our player character.

  16. Use a Get Player Character node and attach as shown below, setting the Location Rule and Rotation Rule pins to Snap to Target.

    Click image for full view.

    This instructs the node that we want to attach the reference variable for our fire effect to the player character, snapping its location and rotation to match the player character.

  17. Click the Compile button to compile the Blueprint.

    SpawnDestroy_17.png

  18. Return to the main Level Editor window, then from the Main Toolbar click the Play button to play inside the editor.

    SpawnDestroy_18.png

End Result

While playing in the editor, when you press the F key the fire effect will spawn in and become attached to the player character. When pressing the F key while the fire is attached to the player, this will cause the effect to be destroyed, and you can press F again to re-spawn it.

Another way you could approach this is to Activate and Deactivate the particle effect contained within the Blueprint Fire effect.

Click image for full view.

In the example above, we spawn in the Blueprint Fire at the start of the game using the Event BeginPlay node and attach it to our player character. We also deactivate the particle effect and fade out the associated sound at the start of the game. When we press F, since the Blueprint Fire effect has already been spawned in (just not activated), we instead activate it and fade in the audio. When we press F a second time, instead of destroying the effect, we deactivate it and fade out the associated sounds.

Select Skin
Light
Dark

Welcome to the new Unreal Engine 4 Documentation site!

We're working on lots of new features including a feedback system so you can tell us how we are doing. It's not quite ready for use in the wild yet, so head over to the Documentation Feedback forum to tell us about this page or call out any issues you are encountering in the meantime.

We'll be sure to let you know when the new system is up and running.

Post Feedback