1 min read

Safe casts in Unreal Engine

Safe casts in Unreal Engine
Photo by Clarissa Watson / Unsplash

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;
}

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.