Destroying actors
Game actors may need to be destroyed when they are killed or no longer needed in a game map. The Pangaea player character, for example, is killed when HealthPoints
reaches 0; once it reaches 0, then we want to remove the player character from the map and kick the player from the game.
To complete this task, we will do three things in the DieProcess()
function:
- Stop the character from ticking. Setting the
PrimaryActorTick.bCanEverTick
variable asfalse
does this. - Call the
K2_DestroyActor()
function to destroy the character. This Unreal actor API destroys the actor and marks the occupied memory for garbage collection, resulting in the release of memory - Call the engine’s
ForceGarbageCollection
function to force the engine to perform garbage collection.
To implement this, we can insert the following three lines of code within the curly braces of the DieProcess
function:
void APlayerAvatar::DieProcess(){ PrimaryActorTick.bCanEverTick...