a
adrianmg

AnimationPlayer behavior

The animation starts playing backward from the current frame when the body exits the collision. How can I force the animation to finish before playing backward?

I'm trying to understand how the animation player logic works.

  • Nathan Lovato replied

    It's actually not trivial to do the way we set up the animation, playing it backward. In this case, I would recommend creating two separate open and close animations and using the function AnimationPlayer.queue() to queue animation playback. If you do that, it will automatically wait for animations to end.

    But you need two animations. The code would look like this:

    func _on_body_entered(_body: Node) -> void:
        anim_player.queue("chest_open")

    func _on_body_exited(_body: Node) -> void:
        anim_player.queue("chest_close")

    This will not give you perfect behavior, as you could quickly move the gem over and away from the chest and queue animations more times than you'd like. You could add a condition to only queue 1 animation at most:

    func _on_body_entered(_body: Node) -> void:
    # This gives us the number of animations currently queued
    var animation_queue_size: int = anim_player.get_queue().size()
    # There's no animation queued, we can queue one
    if animation_queue_size == 0:
    anim_player.queue("chest_open")

    func _on_body_exited(_body: Node) -> void:
    var animation_queue_size: int = anim_player.get_queue().size()
    if animation_queue_size == 0:
    anim_player.queue("chest_close")

    But even then, the behavior wouldn't be perfect because if you were to move the gem very quickly some player inputs would not be registered. By queuing animations you are also inducing gameplay lag where the game takes time to respond to the player's input. So, in this case, I would stick to what we do and the lesson where are the chest instantly closes when removing the gem.

    2 loves
  • a
    adrianmg replied

    Thanks, Nathan! I like the current behavior, I was mostly looking to understand how things work within the animation system.