Skip to main content

Spawn Actor

Official Documentation: docs.unrealengine.com.

Description

The process of creating a new instance of an Actor is known as spawning. Spawning of Actors is performed using the UWorld::SpawnActor() function. This function creates a new instance of a specified class and returns a pointer to the newly created Actor. UWorld::SpawnActor() may only be used for creating instances of classes which inherit from the Actor class in their hierarchy.

Idea

Useful if you want to Spawn an Actor in Runtime.

In this example we Spawn an Actor from another Class.

SomeActor.h
#include "ClassToSpawn.h" // Include actor to spawn

class AYourClass : public AActor
{
...
// define what Class to Spawn
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "YourGame|Actor")
TSubclassOf<AClassToSpawn> ClassToSpawn;

// Reference to the Spawned instance
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "YourGame|Actor", meta = (AllowPrivateAccess = "true"))
AClassToSpawn* SpawnedClass;
...
}
SomeActor.cpp
// for this example we'll just spawn it on BeginPlay
void AYourClass::BeginPlay()
{
// Call the base class
Super::BeginPlay();

// Spawn Actor in the world at Location 0,0,0
if (ClassToSpawn)
{
SpawnedClass = GetWorld()->SpawnActor<AClassToSpawn>(ClassToSpawn, FVector::ZeroVector, FRotator::ZeroRotator);
}
}
Important

Dont forget to select the Actor you want to Spawn in the Details Panel under ClassToSpawn.

This was an example on how to Spawn an Actor in Unreal Engine C++.