+5 votes

I have a script wich plays multible animations after each other, but if I execute it, it plays every animation at once. (Note that the animation sequence is changing everytime it executes)
How can i wait until the animation is finished and then call the next animation?


Thanks for help,
Skayo

in Engine by (37 points)

3 Answers

+4 votes
Best answer

Connect the AnimationPlayer's "finished" signal to a function, that starts the next animation!
For example:

onready var player = get_node("AnimationPlayer")

func _ready():
    player.connect("finished", self, "playNextAnim")
    player.play("anim_1");

func playNextAnim():
    if(player.get_current_animation() == "anim_1"):
        player.play("anim_2")
by (674 points)
selected by

Brilliantly simple. Nice

In Godot 3.3 , the callback is "animationfinished", and passes in a parameter 'animname'... i had to pass that into the playNextAnim above, as my 'player.getcurrentanimation() would return an empty string on finished'

+14 votes

Somewhere In the official docs, there's your answer in the yield function:

yield( get_node("AnimationPlayer"), "finished" )

For your example, it'll look like :

onready var player = get_node("AnimationPlayer")

func do_my_animation_sequence:
    player.play("anim_1")
    yield( player, "finished" )
    player.play("anim_2")
    yield( player, "finished" )
    player.play("anim_3")
    ...
by (703 points)

I found that the line below cannot work now:

yield(get_node("AnimationPlayer"), "finished")

But this code works:

yield(get_node("AnimationPlayer"), "animation_finished")

I test in Godot 3.0.6.

That is because in Godot 3, the "finished" signal has been changed to "animationfinished".
So "finished" should work for Godot 2 and "animation
finished" for Godot 3

Cool, now have a hitting animation! Thanks!

Best answer IMHO, use animation_finished for godot 3.0 upwards

0 votes

You can create a function to lock the animation and call it from the animation track.

var animation_is_active = false

func _process(delta):
    if Input.is_action_just_pressed("ui_attack") and not animation_is_active: 
        animation_player("attack")

# Method called in first key (true) and last key (false) of the animation track
func activate_animation(state): 
    animation_is_active = state
by (261 points)

It's better to use animation_finished() signal.

You are right, but sometimes we only need to get that in one situation, the signal is better, but if you want to play the same animation multiple times, and after each do something different, the yield(get_node("AnimationPlayer"), "animation_finished") is better

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.