6.177: Getting Name Crashes

Hello,

I am trying to get the Owner’s name when the actor dies, just so I can see who is destroyed. However, it crashes everything I try to do so. I have followed what I considered the normal procedure, but to no avail.

void ATankGameModeBase::ActorDied(AActor* DeadActor)

{
    UE_LOG(LogTemp, Warning, TEXT("%s Died!"), *GetOwner()->GetName());
}

However, when I remove the GetOwner code, the problem leaves.

Is this because I am coding this wrong? I am of the opinion that it is because I am trying to call something up from the GameMode (something overhead) , which is supposed to reach down, not up to something the hierarchy.

That is trying to get the owner of the game mode which is most likely nothing so would return nullptr. Dereferencing that would be the cause of your crash.

I assume what you meant was “trying to get the name of the dead actor” in which case

void ATankGameModeBase::ActorDied(AActor* DeadActor)
{
    UE_LOG(LogTemp, Warning, TEXT("%s Died!"), *DeadActor->GetName())
}

Though that could crash if nullptr was passed in. There’s a static member function on AActor that will return the string "NULL" if it receives a null pointer.

void ATankGameModeBase::ActorDied(AActor* DeadActor)
{
    UE_LOG(LogTemp, Warning, TEXT("%s Died!"), *AActor::GetDebugName(DeadActor))
}

Would the first example’s potential null problem be fixed by using :

if (!DeadActor) {return;}

?

Yes, though I think it would make more sense for a non-negated condition.

Okay.

The non-negated condition was what the instructor for the ToonTanks had.

That being said, everything worked. Thanks

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms