Spawning and Destroying an Actor

In this How-To guide, you will create your own Actor to spawn, and aActor Spawner which will spawn your Actor at the click of a button.

Choose your operating system:

Windows

macOS

Linux

Choose your implementation method:

Blueprints

C++

Oftentimes in video games, players are kept engaged by attaining new items or weapons for their character, and sometimes new enemies may even appear for them to defeat.

In your game, you may wish to dynamically add items or enemies to your Level during gameplay, this is called spawning.

In this How-To guide, you will create an Actor class that will be spawned from an Actor spawner at the press of a button. Then, you will create the logic to destroy the spawned Actors at the press of a different button.

![image alt text](image_0.gif)(convert:false)

Creating the Actor to Spawn

In this section, you will create an Actor that contains a Sphere Component, a Static Mesh Component, and a Particle System Component.

  1. Begin by creating a new Third Person Blueprint games project, with Starter Content set to enabled . Name your project SpawnDestroy .

    ![](image_1.png)(convert:false)

  2. From the Content Browser , select the Add/Import button and create a new Blueprint Actor class named Bp_ActorToSpawn .

    ![](image_2.png)(convert:false)

  3. In the Class Defaults , navigate to the Components tab and select Add Component . Then, search for and select the Sphere Component , and drag it over the Scene Component to make it the new Root Component .

    ![](image_3.png)(convert:false)

  4. In the Details panel of the Sphere Component navigate to the Shape category and change the Sphere Radius value to 16.0 .

    ![](image_4.png)(convert:false)

  5. Navigate to the Physics category and check the box next to the Simulate Physics variable.

    ![](image_5.png)(convert:false)

  6. Navigate to the Components tab and select Add Component , then search for and select the Static Mesh Component .

    ![](image_6.png)(convert:false)

  7. In the Details panel, locate the Static Mesh category and select the dropdown arrow next to the Static Mesh variable. Then, search for and select the Shape_Sphere Static Mesh asset.

    ![](image_7.png)(convert:false)

  8. Navigate to the Transform category and from the Scale variable, change the X, Y, and Z values to 0 .25 , 0 .25 , 0 .25. Then from the Location variable, change Z value to -12.0 .

    ![](image_8.png)(convert:false)

  9. Navigate to the Components tab and select Add Component . Search for and select the Particle System Component .

    ![](image_9.png)(convert:false)

  10. Navigate to the Details panel and from the Particles category, select the dropdown arrow next to the Template variable, then search for and select P_Fire .

    ![](image_10.png)(convert:false)

  11. Compile & save your Blueprint.

    ![](image_11.png)(convert:false)

Finished Blueprint

![](image_12.png)(convert:false)

Creating the Actor Spawner

Now that you have created an Actor to spawn, you will create an Actor spawner which contains a custom function to spawn your new Bp_ActorSpawn Blueprint.

  1. From the Content Browser , select the Add/Import button and create a new Blueprint Actor class named Bp_ActorSpawner .

    ![](image_13.png)(convert:false)

  2. From the Class Defaults , navigate to the Components tab and select Add Component . Then, search for and select the Box Component and rename it to SpawnVolume .

    ![](image_14.png)(convert:false)

  3. Right-click the Event Graph , from the Actions context menu, search for and select Add Custom Event , then name it SpawnActor .

    ![](image_15.png)(convert:false)

  4. Drag off of the Spawn Actor Custom Event execution pin and from the Executable actions dropdown menu, search for and select Spawn Actor From Class .

    ![](image_16.png)(convert:false)

  5. From the SpawnActor node Class pin, select the BP_ActorToSpawn class.

    ![](image_17.png)(convert:false)

  6. Click and drag off of the Spawn Transform pin from the Spawn Actor from Class node, then from the Actions dropdown menu search for and select Get Actor Transform .

    ![](image_18.png)(convert:false)

  7. Compile & save your Blueprint.

    ![](image_19.png)(convert:false)

Finished Blueprint

![](image_20.png)(convert:false)

Setting Up Additional Character Action Commands

Now that you have created your two Actor classes, Bp_ActorToSpawn and Bp_ActorSpawner, you will now be tasked with setting up the Third Person Character with the functionality to call the Actor spawner's Spawn Actor function.

  1. Navigate to Edit > Project Settings > Engine > Input , then from the Bindings > Action Mappings category, click the + button to add two additional mappings named SpawnActors and DestroyActors .

    ![](image_21.png)(convert:false)

  2. Set the SpawnActors action key mapping to numeric 1 key, and the DestroyActors Action key mapping to the numeric 2 key.

    ![](image_22.png)(convert:false)

  3. From the Content Browser , navigate to your Third Person Character Blueprint and double-click to open its class defaults, then right-click the Event Graph. From the All Actions for this Blueprint context menu, search for and select the SpawnActors input action.

    ![](image_23.png)(convert:false)

  4. Drag off the Pressed execution pin and from the Executable actions dropdown menu search for and select the Get Actor Of Class node. Then, click the dropdown arrow for the Actor Class and select Bp Actor Spawner .

    ![](image_24.png)(convert:false)

  5. Drag off the Return value pin of the Get Actor of Class node. In the Actions menu search for and select the call function Spawn Actor , then connect the execution pin from the Get Actor Of Class node to the execution pin of the Spawn Actor node.

    ![](image_25.png)(convert:false)

  6. Right-click the Event Graph and from the context menu, search for and select the DestroyActors Input Action Event.

    ![](image_26.png)(convert:false)

  7. Drag off the Pressed execution pin and from the Executable actions dropdown menu search for and select the Get All Actors Of Class node. Then, click the dropdown arrow for the Actor Class and select Bp Actor to Spawn .

    ![](image_27.png)(convert:false)

  8. Drag off the Out Actors Return value pin and from the dropdown menu search for and select the For Each Loop With Break node.

    ![](image_28.png)(convert:false)

  9. Drag off the Array Element pin and from the Actions menu, search for and select the Destroy Actor function, then connect the output execution pin from the Get All Actors of Class node into the Execution Pin of the For Each Loop with Break node .

  10. Next, connect the Loop Body execution pin from the For Each Loop with Break node into the Destroy Actor input execution pin.

    ![](image_29.png)(convert:false)

  11. Compile & save .

    ![](image_30.png)(convert:false)

Finished Blueprint

![](image_31.png)(convert:false)

Setting Up the Scene

Now that you have finished creating the functionality necessary for the Player Character to call the Bp_Spawner's SpawnActor function, you will need to choose the location of the Bp_Spawner in the Level.

  1. From the Content Browser , drag an instance of your Bp_ActorSpawner into the Level.

    ![](image_32.png)(convert:false)

  2. Select PIE (Play-In Editor) to test your Bp_ActorSpawner.

End Result

When testing in the editor, if you press the 1 key, then the Actor Spawner class will spawn its Actor into the Level. If you decide to press the 2 key, then all ActorsToSpawn within the Level will be destroyed.

![](image_33.gif)(convert:false)

[INCLUDE:ProgrammingAndScripting/SpawnAndDestroyActors\CPP]
Help shape the future of Unreal Engine documentation! Tell us how we're doing so we can serve you better.
Take our survey
Dismiss