Destroying an Actor using Destroy and a Timer
This recipe will reuse the GameMode
from the previous recipe, so you should complete it first.
How to do it...
- Make the following changes to the
GameMode
declaration:UPROPERTY() AMyFirstActor* SpawnedActor; UFUNCTION() void DestroyActorFunction();
- Add
#include "MyFirstActor.h"
to the implementation file's includes. - Assign the results of
SpawnActor
to the newSpawnedActor
variable:SpawnedActor = GetWorld()->SpawnActor<AMyFirstActor> (AMyFirstActor::StaticClass(), SpawnLocation);
- Add the following to the end of the
BeginPlay
function:FTimerHandle Timer; GetWorldTimerManager().SetTimer(Timer, this, &AUE4CookbookGameMode::DestroyActorFunction, 10);
- Lastly, implement
DestroyActorFunction
:void AUE4CookbookGameMode::DestroyActorFunction() ...
Get Unreal Engine 4 Scripting with C++ Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.