Spawn and Destroy an Actor with Blueprint

Choose your OS:

Regardless of the game type 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 that have been (or have not been) 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 and 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. In the Class section of the SpawnActor node, 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 and 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 and search for and add the Flip Flop node.

    SpawnDestroy_05.png

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

  6. Left-click and drag off the Pressed pin of the F node 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 and search for and add the Destroy Actor node.

    SpawnDestroy_07.png

    With this node we can destroy any Actor we specify.

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

    SpawnDestroy_08.png

    The second time we press F, the B pin on the Flip Flop 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 Return Value of the Spawn Actor node and select Promote to Variable.

    SpawnDestroy_09.png

    The Return Value, in this case, is the "thing that we spawn" and what we want to store a reference to 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, hold Ctrl then drag-and-drop the CurrentFlames variable into the graph and connect it to the Target of Destroy Actor.

    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 and 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. Off the Return Value of Get Player Character, 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 Return Value of the Get Actor Transform to the Spawn Transform on the Spawn Actor node.

    Click image for full view.

    Our graph at this point is almost complete and we designated that when we press the F key that we will spawn in our Blueprint Fire at the location of our player and store it as a variable. When we press F a second time, we will then destroy that variable that 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. Left-click and drag off the reference pin on the Set node and 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 to Snape to Target.

    Click image for full view.

    Here we are stating 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 where 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.