When using the Unreal Engine Editor, you may get a reference for an AActor Type object but want to use it as another class such as a Spacecraft. This requires a cast to turn the object into the target class.

NOTE

If class B inherits from class A, a cast from B to A is not required within the Blueprint graph as B will contain functions and variables from A. However, an explicit cast is needed if an object is of class A that requires functionality from B.


Casting Unreal Objects

You can cast Unreal objects as other types by first searching the type you want to cast it as. For this example, the spacecraft variable is stored as a Spacecraft type but is a BP_Z_Spacecraft_6U_Empty actor. To use the functions and variables stored in BP_Z_Spacecraft_6U_Empty, you will need to cast them as such. Drag from the Spacecraft variable and select cast to the target type.

image 1.png

image.png

You can also right-click the node and select Convert to Pure Cast at the bottom of the menu to make the node more compact and if you don’t need the execution pin if the cast fails.

image.png

NOTE

A blue note will appear at the bottom of the cast if the object is already the same as the base type. If this has been stored this way, a cast isn’t necessary.

image.png

After the cast, you can access the casted class function and variables.

image.png


Casting Zendir Components

Often, spacecraft and other entity actors will have child actor components attached when setting up the classes in the blueprint editor. Rather than requiring you to fetch the actor component from the spacecraft and then perform the cast, a series of functions have been created to perform that for you. These are under the Zendir | Child Actor Component Casting category and can work for any of the base component types.

Untitled

NOTE

It is best practice to store the reference to an object as the lowest base-level class required for variables. For example, rather than storing a reference to the BP_Z_ReactionWheels_Triad class, a reference to the Reaction Wheel Array is preferred as it enables easy swapping of the exact model without causing compile issues requiring a class swap. If functionality from the inherited class is not required, then the storing of that class should be avoided.