Skip to main content

06 - Enemy Spawner

A Tick-driven spawner: an array of enemies, a running spawn-cooldown, and a BlueprintImplementableEvent to let designers decide what a spawn "means".

Full source​

include "CoreMinimal.h"
include "GameFramework/Actor.h"

uclass AExampleSpawner : AActor meta: api=EXAMPLES_API
{
prop public TArray<AActor*> Enemies EditAnywhere BlueprintReadWrite category="Example"
prop public float SpawnInterval EditAnywhere BlueprintReadWrite category="Example" default=2.0f
prop private float ElapsedTime

method public virtual void BeginPlay() override {
Super::BeginPlay();
PrimaryActorTick.bCanEverTick = true;
}

method public virtual void Tick(float DeltaTime) override {
Super::Tick(DeltaTime);

ElapsedTime += DeltaTime;
if (ElapsedTime >= SpawnInterval)
{
OnSpawnEnemy();
ElapsedTime = 0.0f;
}
}

func public void OnSpawnEnemy() BlueprintImplementableEvent category="Example"
}

Line by line​

  • prop public TArray<AActor*> Enemies - an array of actor references you can populate in the Details panel.
  • prop public float SpawnInterval ... default=2.0f - seconds between spawns.
  • prop private float ElapsedTime - internal accumulator (private, invisible to BP).
  • BeginPlay - turns on ticking.
  • Tick - accumulates ElapsedTime; when it crosses SpawnInterval, calls OnSpawnEnemy() and resets.
  • func public void OnSpawnEnemy() BlueprintImplementableEvent - designers implement this in Blueprint. TPScript provides the trigger; Blueprint provides the "what to spawn + where" logic. This is the clean separation of "system" from "design decision".

The data-driven hand-off​

The C++ owns timing; Blueprint owns what happens. In Blueprint you'd:

  1. Right-click the spawner actor → Add Event → OnSpawnEnemy.
  2. Spawn a pawn from your pool, or SpawnActor, at a random location.

That keeps the C++ generic - you can re-skin the spawner without touching TPScript.

Pattern value​

This is the go-to pattern for spawn rate / waves / timers that call a designer hook. Replace Tick + a manual counter with a SetTimer if you prefer not to tick every frame (see Timer instead of Tick).

Next: 07 - Data-Driven Weapon.