Editor: Casting Classes
Description
When using the Unreal Engine Editor, you may have a reference for an object such as an Actor
but want to use it as a a child class. This requires a cast from the actor class to the base class. Casting can also be useful for testing if the actor is of a certain type of class.
Note
If class B
inherits from class A
, then a cast from B
to A
is implied and not required within the Blueprint graph as it’ll contain functions and variables from A
. However, if class A
requires functionality from B
then an explicit cast is required.
Casting Unreal Objects
You can cast Unreal objects as other types by first searching the type you want to cast it as. For example, take the spacecraft class BP_Spacecraft_Empty_6U
. This class inherits from the Spacecraft
class. To store the reference of the class in the base format, a Cast To Spacecraft
function needs to be called.
You can also right-click the node and select Convert to Pure Cast
at the bottom of the menu to make the node look cleaner and if you don’t need an execution if the cast fails.
Note
A blue note will appear if the object already is the same type as the base type. If this has been stored this way, then a cast is no longer necessary.
After the cast, the graph will have access to the casted class function and variables.
Casting Nominal 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 Nominal Systems | Child Actor Component Casting
category and can work for any of the base component types.
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_NS_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.