Skip to main content

Spawn an actor

Spawn a projectile/enemy/prefab at the actor's location, or at an explicit transform.

At your own location​

uclass AShooter : AActor meta: api=MYGAME_API
{
prop public TSubclassOf<AActor> ProjectileClass EditAnywhere BlueprintReadWrite

func public void Fire() BlueprintCallable
{
if (ProjectileClass)
{
GetWorld()->SpawnActor<AActor>(ProjectileClass,
GetActorLocation(), GetActorRotation());
}
}
}
  • GetWorld()->SpawnActor<T>(Class, Location, Rotation) is the workhorse.
  • TSubclassOf<AActor> ProjectileClass lets designers pick the spawnable class in the Details panel (self-structuring, designer-safe).

Using the spawn sugar​

when_begin
{
spawn enemy: enemy from spawn_location
enemy->StartAI();
}

spawn x: T from location expands to SpawnActor with x's class and the given location (or your current location when location is omitted). Requires the spawn_location variable/expression in scope.

At an explicit transform​

FTransform SpawnTrans = GetActorTransform();
SpawnTrans.SetLocation(AimPoint);
GetWorld()->SpawnActor<AActor>(ProjectileClass, SpawnTrans);