DEV Community

Tiny
Tiny

Posted on • Originally published at tinygame.dev on

Safe casts in Unreal Engine

Actor to Character

Say you have a pointer to an Actor and you'd like to cast this to a Character so you can call your function. Unreal has the Cast function template to do this safely:

ACharacter* Character = Cast<ACharacter>(GetOwner());
if (!Character)
{
    return;
}
Enter fullscreen mode Exit fullscreen mode

In the above case GetOwner() returns an AActor* which the Cast turns into a nullptr if it's not an ACharacter (or a child) object.

That is all.

Top comments (0)