How to trigger state machines with button presses

Hi there!
I would like to know how am I supposed to change variables from within my animation blueprint with a button press.
Right now I have this set as my animation blueprint:

I have a variable called “isAttacking?” that I want to use to enable/disable attacking, but I’m not just how can I set it to true or false by pressing a button from within the blueprint or if I can do it from my character blueprint instead.
Can anyone help me solve this? Thank you.

I’d be interested in knowing this too. I just started working with blueprint.

A common pattern would be something like this:

  • Add an ‘IsPawnAttacking’ variable to your pawn
  • Set IsPawnAttacking inside the pawn code/blueprint when you press a key
  • Inside the Event Graph of your AnimBlueprint, get your Pawn Owner, cast it to the class you expect (the one that contains the IsPawnAttacking variable)
  • Copy the value of IsPawnAttacking from the pawn to the IsAttacking variable in your AnimBlueprint so it can be used by the state machine

How do you cast it? I have a “MyCharacter” blueprint which is the one with the components and the controls and then I have an animation blueprint for the state machines called “PlayerCharacter”.
So, I’m supposed to cast MyCharacter to PlayerCharacter so I can access “IsPawnAttacking” and set its value to “IsAttacking” inside PlayerCharacter. However, I don’t know how you do the casting.

I’m nowhere near the pro at this that James is, but I think I may be able to help.

I think the step that is confusing you is where/when you need to do the casting. In your Animation Blueprint, you will need to cast the AnimBP owner to your particular Pawn BP. From there, you can access whatever variables may be setting in your Pawn BP. I did a quick example:

692cefc0b0907c55278d285b836ed71154220c23.jpeg

What my Pawn BP looked like:

Good luck!

From “GetOwner” output, drag left mouse off and you should be able to see context menu coming up. There either search your blueprint class name or cast, and it should show. Hope that works.

The problem is that I don’t know how to cast.

I don’t get any cast option, or maybe I just don’t know how to do something so simple. Obviously I need to cast my CharacterBP to my AnimationBP so my AnimationBP can access isPawnAttacking and set isAttacking to the same value. Am I supposed to do anything special within the CharacterBP to make it accessible within the AnimBP?

Check Lina’s post. In order to get a Cast node, you’ll need to drag off of the “Try Get Pawn Owner” node in your AnimationBP. Because it’s context sensitive, the Cast options won’t show up unless you are dragging off of a valid target.

You shouldn’t need to do anything special to your CharacterBP.

Ahhh, I get it now. I didn’t know that some nodes can only be accessed from links from other nodes. I managed to get it working.
Thank you guys! I think this thread can be closed already.

Sorry to resurrect a dead post but I’m still lost here. I got everything working as shown above but I can’t figure out how to access the IsAttacking? variable inside the state machine now that its set in the Event Graph. James said “Copy the value of IsPawnAttacking from the pawn to the IsAttacking variable in your AnimBlueprint so it can be used by the state machine” How do you do that?

Here’s my setup:
305d7e145632f44e76109821a87f3bf96e60a799.jpeg

Also I don’t understand why you need to Cast to Character and then Cast to Player Pawn. Isn’t that redundant?

I should specify what I’m trying to do is cause a skeletal mesh with a door to move between the various states of Closed, Opening, Open. Maybe there’s a more simple way to do this?

Anyone able to help answer this question? Its really frustrating to get this far and then be stumped. Also irritating when someone says “hooray I figured it out!” but then doesn’t share the answer :stuck_out_tongue:

I am sure you probably solved this by now but what you have create a variable IN your animblueprint and then you SET it with the cast to character->(Character Blueprint variable) once you have done that you use the variable that you have IN the animblueprint in your state machines.

example: Character blueprint variable(bool) = IAMSHOOTING(true) Anim Blueprint variable(bool) = SHOOTING in animblueprint casttocharacter->target-IAMSHOOTING ->(SET)SHOOTING in state machine SHOOTING = true ->can enter transition

hope this helps or makes sense

DJMK