Checking an Actor instance’s actual class type
In Unreal, the OOP approach regularly utilizes inheritance to declare class relations. Even though inheritance has the advantage of code reuse, it has the limitation of storing and returning instances while only knowing the base class types.
Based on the inheritance design pattern, type casting very often occurs in OOP. An example in Unreal scripting is that when a collision triggers an overlap event, the two parameters of the event function are both AActor*
type pointers, whereas they should be of the APlayerAvatar*
, AEnemy*
, or ADefenseTower*
type.
To find out whether AActor*
is another child class type pointer, we use the Cast
function to cast the AActor*
pointer to the required type of pointer (APangaeaCharacter*
, for example) and check whether the result is nullptr
or not. If the Cast
operation fails, it means that the actor is not the type of actor we need:
void AWeapon::OnWeaponBeginOverlap(AActor* OverlappedActor...