derkork / godot-statecharts

A state charts extension for Godot 4
MIT License
761 stars 39 forks source link

How do I travel to a state when a animation ends? #25

Closed Shadowblitz16 closed 1 year ago

Shadowblitz16 commented 1 year ago

How do I use animation player states and transitions to travel to a state when the animation is over?

derkork commented 1 year ago

When an animation player state is activated it will automatically start the configured animation in the linked animation player. The animation player has a signal animation_finished which you can listen to and then send an event to the state chart. Then a transition inside the animation player state can listen to this and transition elsewhere.

image

In this example I have two states one plays an animation RotateLeft and the other one plays an animation RotateRight. Both states have a transition to the other state which listens on the animation_player_finished event. I also have connected the animation_finished signal of the animation player to a function which will send the animation_player_finished event to the state chart:

image

Now when the scene starts this happens:

https://github.com/derkork/godot-statecharts/assets/327257/49af4999-0096-4c1a-8811-a9c1b62fecdc

Here is the scene for you to try out: test.tscn.zip

I hope this helps.

Shadowblitz16 commented 1 year ago

thankyou

Phanterm commented 11 months ago

I would like to add to this, for those of you who do not use Animation Player States but would like similar functionality.

image

I've had good luck so far with adding a Call Method track on the last frame of an animation I want to transition to another state by invoking the send_event function with a StringName argument containing whatever value would cause the transition from the current state.

image

Hope that's helpful to someone!