For this recipe, we will create a building that spawns units at a fixed time interval at a particular location.
Create a new
Actor
subclass in the editor, as always, and then add the following implementation to the class:UPROPERTY() UStaticMeshComponent* BuildingMesh; UPROPERTY() UParticleSystemComponent* SpawnPoint; UPROPERTY() UClass* UnitToSpawn; UPROPERTY() float SpawnInterval; UFUNCTION() void SpawnUnit(); UFUNCTION() void EndPlay(const EEndPlayReason::Type EndPlayReason) override; UPROPERTY() FTimerHandle SpawnTimerHandle;
Add the following to the constructor:
BuildingMesh = CreateDefaultSubobject<UStaticMeshComponent>("BuildingMesh"); SpawnPoint = CreateDefaultSubobject<UParticleSystemComponent>("SpawnPoint"); SpawnInterval = 10; auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Cube.Cube'")); if (MeshAsset.Object != nullptr) { BuildingMesh->SetStaticMesh...