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())
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.
But, what is the difference between signal in documentation (animation_finished(StringName anim_name) and signal animation_finished alone without bracket and parameters ?
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
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