Dynamic Casting Best Practice

In tutorials (even the high quality ones) it is common to see something like this



    void AFPSObjectiveActor::NotifyActorBeginOverlap(AActor * OtherActor)
    {
         AFPSCharacter* OverlappedCharacter = Cast<AFPSCharacter>(OtherActor);
         if (OverlappedCharacter)
         {
             //....
         }
    }


but this is not very common in general games code (in my experience). Is this just tutorials skipping things for simplification or is this just the “UE4 way” of doing this kind of thing? Are there any common alternatives?

… general games code? what else would you do for a similar sort of operation?

1 Like

Casting is very common.

1 Like

In other code bases I have worked on this situation is very rare (because inheritance was not used so often and when it was used it was in situations when the base class interface was sufficient).

When absolutely sure the pointer won’t be null (or should never be) I just use ->IsA() instead; but dynamic casting is safer to use.

AActor is a very high-level class. It can be potentially be everything that is placed in the world.
Casting is a necessity to determine it’s actual type.

However, there is the alternative of Components. You can just use GetComponentOfClass<T>() to retrieve a Component and use the Component to execute your logic.
This is type-safe (but not null-safe of course). It is also questionable whether this is faster than casting (prolly not)

1 Like

The reasons for casting in UE4 is because Inheritance is heavily used, mainly due to Garbage Collection / Serialization. UObject is the base class for the majority of all objects within UE4, and thus you have to cast from that to something else.

1 Like

so… you’re looking for an alternative that is more similar to something that doesn’t apply at all? i’m very confused.

1 Like

Thanks for all the replies, very helpful :slight_smile: I’m just getting used to the unreal way of doing things :smiley: I was just making sure because dynamic casting is quite slow for an operation that doesn’t involve fetching memory so often avoided in performance sensitive code and too much dynamic casting can be a code smell. It sounds like here the benefits gained from garbage collection, serialization and common interface greatly outweigh this (minor) inconvenience.

Not at all, I’m just checking that I am not getting into bad habits early on :slight_smile: As I said dynamic casting is often avoided where possible in the wider C++ community so I need to learn how to use Unreal and unlearn some of the things I “know”. Since I’m new to the engine I sometimes get things to work but not sure if I am doing it the “proper” Unreal way or if I have just performed an ugly hack.

Thanks again!

1 Like

You’re right that in normal C++, RTTI and casting are pretty performance heavy. However, UE4 has it’s own reflection system so casting is much cheaper. You shouldn’t need to worry about casting performance in UE4 (I have yet to run into a profile where casting was the bottleneck).

3 Likes