On_animation_ player_animation_finished not working

Godot Version

4

Question

I am trying to delay jumping until the jump animation has finished, when i run the code my message has not been printed nor has my character jumped. My code looks like this

extends CharacterBody2D


const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@onready var animated_sprite = $Sprite2D/AnimationPlayer
@onready var jump_anim: AnimationPlayer = $Sprite2D/AnimationPlayer







# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")





func _physics_process(delta):
    # Add the gravity.
    if not is_on_floor():
        velocity.y += gravity * delta
   
       
        
        
    # Get the input direction and handle the movement/deceleration.
    # As good practice, you should replace UI actions with custom gameplay actions.
    var direction = Input.get_axis("MoveLeft", "MoveRight")
    if direction:
        velocity.x = direction * SPEED
        
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)
        
    if velocity.x > 0 and velocity.y == 0:
        $Sprite2D.flip_h = false
        animated_sprite.play("Run")
    if velocity.x < 0 and velocity.y == 0:
        animated_sprite.play("Run") 
        $Sprite2D.flip_h = true               
    if velocity.x == 0 and velocity.y == 0:
        animated_sprite.play("Idle")
    if Input.is_action_just_pressed("Jump") and is_on_floor():   
        animated_sprite.play("Jump") 
        velocity.y = JUMP_VELOCITY
        print("space") 
    
        
           
   
       
    # Replace with function body.   
    move_and_slide()       
    








func _on_animation_player_animation_finished():
    if ($AnimationPlayer.animation == "Jump"):
        velocity.y = JUMP_VELOCITY
        print("Yipeee")
        
func _on_AnimationPlayer_animation_started(Jump):
    pass

func _on_AnimationPlayer_animation_finished(Jump): 
    pass

I am receiving this error as well 
E 0:00:03:0136   emit_signalp: Error calling from signal 'animation_finished' to callable: 'CharacterBody2D(PlayerMovement.gd)::_on_animation_player_animation_finished': Method expected 0 arguments, but called with 1.
  <C++ Source>   core/object/object.cpp:1140 @ emit_signalp()

this just signaling issues, need to see how do you emit signal? i dont see it here?
the target callable/method expects 0 argument, but the signal emits 1 arguments, hence the error
to fix this
connect the animation_finished signal like this animation_finished.connect(func(x):_on_animation_player_animation_finished())

Hello the community,

I am a newbie in godot and i am trying to learn with documentation & youtube channel.

So, i have a similar comportment on this signal.
In documentation it works with 1 argument, but not in fact (for me).

ko

@onready var animation_player:AnimationPlayer = $AnimationPlayer

func teleport()-> void:
	animation_player.play(anim)
	await animation_player.animation_finished("fade_in")
	get_tree().change_scene_to_packed(next_scene)

With an error message: Name: “animation_finished” called as a function but is a “Signal”.

ok

@onready var animation_player:AnimationPlayer = $AnimationPlayer

func teleport()-> void:
	animation_player.play(anim)
	await animation_player.animation_finished
	get_tree().change_scene_to_packed(next_scene)

For me, this line is like a call function: animation_player.animation_finished

in documentation

Signals:
animation_finished(StringName anim_name)

  • For my undestanding, is it a bug or mine mistake ?

Thanks

As far as I understand await just waits until the animation_finished signal is emitted for any animation. You can’t pass an argument to wait for a specific animation.

This means if you connect the signal to a function, it’ll pass the finished animation’s name to the function when emitted.

2 Likes

Thank your answer.

You’re rigth!! :slight_smile: have try this, and i got the name of animation: nice

func _on_animation_player_animation_finished(anim_name):
	print(anim_name)

But, what is the difference between signal in documentation (animation_finished(StringName anim_name) and signal animation_finished alone without bracket and parameters ?

Thank

2 Likes

Nothing, it’s the same signal. You just don’t have to use brackets and can’t pass parameters when you’re waiting for a signal with await.
It’s just how it works, I don’t know why :slight_smile:

edit: actually if I remember correctly you can get the parameters (anim_name) like this, but not sure, can’t test it right now:

var anim_name = await animation_player.animation_finished

1 Like

Blockquote edit: actually if I remember correctly you can get the parameters (anim_name) like this, but not sure, can’t test it right now:

var anim_name = await animation_player.animation_finished

It’ s work well too :slight_smile:

thanks

2 Likes