diff --git a/Source/EventCenterPlugin/Private/SpawnableActor.cpp b/Source/EventCenterPlugin/Private/SpawnableActor.cpp index 4f8a582..1be48f5 100644 --- a/Source/EventCenterPlugin/Private/SpawnableActor.cpp +++ b/Source/EventCenterPlugin/Private/SpawnableActor.cpp @@ -43,3 +43,14 @@ void ASpawnableActor::Tick(float DeltaTime) { Super::Tick(DeltaTime); } + + +void ASpawnableActor::StartDestroyTimer(float seconds) +{ + GetWorldTimerManager().SetTimer(DestroyTimer, this, &ASpawnableActor::InvokeDestroy, seconds); +} + +void ASpawnableActor::InvokeDestroy() +{ + Destroy(); +} diff --git a/Source/EventCenterPlugin/Private/SpawningActor.cpp b/Source/EventCenterPlugin/Private/SpawningActor.cpp index 6dc69bc..b1ec3aa 100644 --- a/Source/EventCenterPlugin/Private/SpawningActor.cpp +++ b/Source/EventCenterPlugin/Private/SpawningActor.cpp @@ -61,5 +61,7 @@ void ASpawningActor::Spawn() // TODO(bt): This will memory leak. ASpawnableActor* spawnedActor = (ASpawnableActor*)GetWorld()->SpawnActor(ASpawnableActor::StaticClass()); spawnedActor->SetActorLocationAndRotation(location, rotation, false, 0, ETeleportType::ResetPhysics); + + spawnedActor->StartDestroyTimer(10.0); } diff --git a/Source/EventCenterPlugin/Public/SpawnableActor.h b/Source/EventCenterPlugin/Public/SpawnableActor.h index 9b46f01..c4e7074 100644 --- a/Source/EventCenterPlugin/Public/SpawnableActor.h +++ b/Source/EventCenterPlugin/Public/SpawnableActor.h @@ -3,6 +3,7 @@ // Unreal Core #include "CoreMinimal.h" #include "UObject/NoExportTypes.h" +#include "Engine/EngineTypes.h" // EventCenterPlugin #include "SpawnableActor.generated.h" @@ -22,11 +23,14 @@ public: /** Mesh to use */ UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SpawnableActor", meta = (AllowPrivateAccess = "true")) - class UStaticMeshComponent* BaseMeshComponent; + class UStaticMeshComponent* BaseMeshComponent; /** Mesh to use */ UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SpawnableActor", meta = (AllowPrivateAccess = "true")) - class UStaticMesh* BaseMesh; + class UStaticMesh* BaseMesh; + + /** Set a timer to destroy this actor. */ + void StartDestroyTimer(float seconds); protected: // Called when the game starts or when spawned @@ -34,6 +38,12 @@ protected: virtual void BeginDestroy() override; + // Destroy timer callback. + void InvokeDestroy(); + + // (Optional) destroy callback timer. + FTimerHandle DestroyTimer; + public: // Called every frame virtual void Tick(float DeltaTime) override;