Cast to Character Blueprint inside Animation Blueprint

I spent 4 hours yesterday searching everything from Unreal documentation to YouTube videos, answers.unreal.com (before it blew up and stopped working), on a question I see posted by hoards of confused game designers: How do I get a Character Blueprint reference inside my Animation Blueprint, so I can access my custom character blueprint variables?

The Unreal documentation doesn’t give an example to do this. What everyone tries to do is drag from Try Get Pawn Owner to a Cast To custom_player_BP, but of course this fails. So you start trying a cast to Character, you try Get Pawn, you start trying everything, then slam your head into the desk and lie on the floor for an hour, wondering how this stupid task is so hard. You can watch it fail just by connecting the Cast Failed pin from the Cast when object is connected to Try Get Pawn Owner.

Well I just started guessing and THIS WORKS! I finally got access to my Player BP and variables. I resorted to guessing because literally nobody had a direct example.

Try Get Pawn Owner isn’t the right source is the best I can figure, but Get Owning Actor from the Event Graph seemed to be exactly what is needed to then Cast to your character blueprint.

If I’m NOT doing the right thing and you have deeper insight than I do into this, please let everyone here know – but this is working so far!

I really hope this helps others in the same boat!

Maybe its just lack of knowledge for these game designers, Try Get Pawn Owner works just fine and it will depend where your calling the cast also, you should be calling the cast on Event blueprint initialize animation then set a variable of your character class there rather than casting to it every time.

Also how is this related to UE5 ?

Try Get Pawn Owner cast to Player Character Blueprint fails from an Animation Blueprint Event Graph.

Then the issue is your end my friend, try logging the class off the pin.

Now you have me curious. I definitely don’t want to leave a bad piece of advice up top for anyone else.

When I print the “Try Get Pawn Owner” I get the name of my Blueprint. I can call functions directly on it such as GetActorRotation, GetVelocity etc without any casting. The problem here is I cannot access a custom variable that is defined on the Blueprint (in this case “Speed” or a new “Health” variable I’ve added.

If I try to drag a wire from the Blueprint var target pin to Try Get Pawn Owner, the editor shows an error, “BP_Player object reference is not compatible with Pawn Object Reference”.

If my method of Get Owning Actor is wrong to cast to the BP then get the variables set there, how would you access the character blueprint’s variables by starting with “Try Get Pawn Owner?”?

1 Like

The whole thing is bad advice.

It’s supposed to go the other way around.
Animation BP is to have no code.

Character casts to animation BP and sets variables.

Google
Ue4 Animation FastPath

For reasons why.

MostHost_LA is right you should only use the Anim BP for getters not setters, use it to drive your animations states.

But you will still need to cast to your character class, and for these custom variables have you set them to private? as you wont be able to access them if you have.

Example below of how you should cast to your character.
https://gyazo.com/87c67d0d8232294bfd0afe48f45255b0

Up voted for “truth”. A animation BP should never be used as a coding extension but as a means for what ever is used to set the current required animation stae. Ideal the controller should be telling the animBP what is required and not have the animBP play 20 questions

@OverRated_AU @MostHost_LA Thank you guys, if that’s the case, then I’ve definitely gotten some bad advice from a few tutorials. I’ll give that rework a try and check out those links you posted!

I found out my Preview Mesh said “The mesh has changed and needs updating”:

image

I also followed @OverRated_AU screenshot and after setting that up, I’m getting this:

But I notice that’s just for one of the debug instances, there are 3 debug instances listed. So now I need to figure out what the difference is between the various instances - not all of them pass the IsValid check.

Update: The other instance was a blueprint I had dropped into the world but wasn’t the one that spawned on PlayerStart - the active mesh! Thus, looking at Debug for the wrong object shows obviously that the IsValid? is false, because that isn’t attached to a player at that moment.

Thanks guys for all your help!

1 Like

For consideration

I noticed that you are casting for “is Flying” and “Is Falling” as a Boolean value from what I assume is your controller. As a state change they are part of a logic block usually called “Locomotion”. Since the animation can only be in a single state at any given time, and that state requirement has been determined by the controller, you could assign the current state as a single value in the controller as a token value and past the single value onto the animation BP.

Goes to the idea that the animation BP should only trigger the desired state change and avoid a bottle neck if there is a need for dozens of state changes and since the controller knows the current state as an absolute there is no need for twenty questions .

Is flying and Is Falling aren’t necessarily the same thing.
Nor, are they necessarily mutually exclusive.
Probably depends 100% on the type of game you are going for.

The better advice would probably be to completely separate the blueprints for flying and walking… :stuck_out_tongue: