Cast To X¶
At a glance
Lives in: Blueprint API / Utilities / Casting - one node is generated
for each target class, such as Cast To Character or Cast To BP_Hero ·
Returns: the same object, viewed as the target type, or None on the
failed path ·
Fails by: taking the Cast Failed exec pin silently - no error, no log,
no conversion ·
Official examples: Cast To Character
and Cast To Object Class
The one-minute version¶
- A cast is a runtime type check. It asks, "is this object actually this class, or a child of this class?"
- It does not turn one object into another. If the object is your
BP_HeroCharacter, theAs BP_HeroCharacterpin is still that same object. - Cast down the class ladder when a broad pin, such as
Pawn, is carrying a more specific object, such as yourCharacter. The ladder is explained in The gameplay class hierarchy. - Failure is normal. A failed cast means "the object was
None, or it was not that class." It does not run hidden cleanup code. - In an Animation Blueprint, the classic beginner pattern is:
Try Get Pawn Owner->Cast To BP_YourCharacter-> store the result in a variable.
What it actually does¶
Blueprint pins often use a broad type because the engine cannot know your
project classes ahead of time. Try Get Pawn Owner returns Pawn, not
BP_HeroCharacter, because the engine only knows that an Animation Blueprint
may be animating some kind of pawn.
Cast To BP_HeroCharacter is the step where your graph says:
"I expect this pawn to really be my hero class. If it is, let me use the hero-specific variables and functions."
pseudocode of the engine's behavior - not engine source
function CastToHero(Object):
if Object is None:
AsHero = None
go to Cast Failed
if Object's real class is BP_HeroCharacter,
or a child class of BP_HeroCharacter:
AsHero = Object // same object, exposed through the Hero pin
go to success
AsHero = None
go to Cast Failed
Nothing about the object changes. The cast only changes what Blueprint lets you
see on the output pin. Before the cast, a Pawn pin can show pawn-level
functions. After a successful Cast To BP_HeroCharacter, the output pin can
also show the variables and functions you added to BP_HeroCharacter.
This is why beginners see casts everywhere after nodes like Try Get Pawn Owner: the first node gets "my pawn," and the cast proves whether that pawn is the specific class the rest of the graph expects.
When it fails (and what failure does)¶
A cast fails when the input object is empty or when its real class is outside the target class family.
Common causes:
- The input is
None. In an Animation Blueprint preview,Try Get Pawn Owneroften returnsNone, so the cast fails before play begins. That is editor-preview behavior, not proof your runtime character is broken. - You are holding a different object than you think.
Get Player Charactergets a local player character; it does not get the enemy, projectile, or overlap actor unless that object really is the local player character. - The object is a sibling class, not a child class.
BP_WolfandBP_HeroCharactercan both inherit fromCharacter; neither is a child of the other, so casting a wolf to your hero fails. - You cast too far down the ladder. A generic
Pawnthat is a vehicle, drone, or turret will not cast toCharacterunless that pawn actually inherits fromCharacter.
Failure does not throw an exception, crash the game, print a log, or call a
secret handler. Execution simply leaves through Cast Failed, and the As X
object is empty.
One important edge: a cast answers type, not lifetime. A stored reference can be the right class and still no longer be safe because the actor is being destroyed. Use Is Valid when the question is "can I still use this reference?"
The pattern everyone actually uses¶
For the founding Animation Blueprint case:
Event Blueprint Initialize Animation ->
Try Get Pawn Owner -> Cast To BP_YourCharacter ->
promote As BP_YourCharacter to a variable like OwnerCharacter.
Event Blueprint Update Animation ->
check OwnerCharacter with Is Valid ->
read velocity, grounded state, weapon state, or other cheap animation inputs.
That pattern keeps the type check out of the every-frame graph. A single cast is cheap enough for normal gameplay, but re-casting every frame makes the graph noisier and keeps more work in the animation Event Graph. Cache the relationship once, then make your update graph read already-known data.
The C++ twin, for the curious (our own example code):
void UHeroAnimInstance::NativeInitializeAnimation()
{
Super::NativeInitializeAnimation();
OwnerCharacter = Cast<AHeroCharacter>(TryGetPawnOwner());
}
Lookalikes - which one do I want?¶
| Node | Question it asks | Use when |
|---|---|---|
| Cast To X | "Is this object really X, or a child of X?" | You need members that only exist on that specific class. |
| Is Valid | "Is this object not None and not pending destruction?" |
You already have a reference and need to know whether it is still safe to use. |
== None |
"Is this variable empty?" | You only care whether the reference is empty, not whether the object is being destroyed. |
| Does Object Implement Interface | "Can I send this object an interface message?" | Many unrelated classes should respond to the same command without sharing a parent class. |
| Class Is Child Of | "Is this class asset under that parent class?" | You are comparing class types, not checking a live object instance. |
Rule of thumb: cast when your graph is honestly tied to one class. Use an interface when several unrelated classes should support the same behavior.
Going deeper¶
- The gameplay class hierarchy - why a
Pawnpin can hold yourCharacter, and why a cast exposes your custom class members. - Try Get Pawn Owner -
the common Animation Blueprint node that hands you a broad
Pawnpin. - Is Valid - the lifetime check that often comes after a successful cast when a reference is stored.
- Ownership and lifetime - why type and validity are separate questions.
- Engine source (requires engine access - we do not reproduce it here):
UK2Node_DynamicCastinEngine/Source/Editor/BlueprintGraph/Private/K2Node_DynamicCast.cpp, and the runtimeCast<>()helper inEngine/Source/Runtime/CoreUObject/Public/Templates/Casts.h.