Referencing destroyed objects

I am uncertain as how to go about checking if an enemy in my game has been destroyed or not.

For example, in my character class I have a reference AActor * currentEnemy which represents the current enemy
to attack. When the enemy’s health has gone below 0, it is destroyed via Destroy().
In my character class, how am I able to check if the currentEnemy is destroyed(alive) or not, without doing this:

Note that this is just random example code:


void attackEnemy()
{
   if (currentEnemy != NULL)
   {
      Enemy * customEnemy = Cast<Enemy>(customEnemy);
      if (customEnemy->getHealth() <= 0)
      {
         currentEnemy = NULL;
      }
   }

Hello you can check if a Actor is marked for destruction with the following methods.



[AActor::IsPendingKillPending](https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/IsPendingKillPending/index.html)
[UObjectBaseUtility::IsPendingKill](https://docs.unrealengine.com/latest/INT/API/Runtime/CoreUObject/UObject/UObjectBaseUtility/IsPendingKill/index.html)
[UObjectBase::IsValidLowLevel](https://docs.unrealengine.com/latest/INT/API/Runtime/CoreUObject/UObject/UObjectBase/IsValidLowLevel/index.html)


Also before you use any new pointer you check it to see if its valid or not.


if ((customEnemy) && customEnemy->getHealth() <= 0) // If valid and less then or equal to 0
{
      // e.g:
      customEnemy->IsPendingKillPending(); // true if this actor has begun destruction, or if this actor has been destroyed already. 
      customEnemy->IsPendingKill(); // Checks the RF_PendingKill flag to see if it is dead but memory still valid
      customEnemy->IsValidLowLevel(); // true if this appears to be a valid object
}

Hope it helps.

1 Like