im making a game in godot 4.2. you cant use yield anymore its await now. im trying to play an animation for an ability kind of like how it's done in overwatch. but my code is wrong, as the ability cooldown plays infinitely! fix my code to make it stop: await get_tree().create_timer(secondary_animated_sprite.get_animation("ability_cooldown").length).timeout secondary_animated_sprite.stop()
In Godot 4.2, the await
keyword is used to pause execution until a signal is emitted or a promise is resolved. In your case, it looks like you're trying to play an animation for an ability cooldown and then stop it after the animation finishes. However, the way you're currently using await
might not be correctly handling the animation playback.
Here's a revised version of your code that ensures the animation plays once and then stops correctly:
# Start the ability cooldown animation
secondary_animated_sprite.play("ability_cooldown")
# Wait for the animation to finish
await get_tree().create_timer(secondary_animated_sprite.get_animation("ability_cooldown").length).timeout
# Stop the animation after the cooldown
secondary_animated_sprite.stop()
play
method is called to start the animation.await
statement waits for the timer to finish, which is set to the length of the animation.stop
method is called to stop the animation.play
.Here’s an example of how you might check if the animation is already playing:
if !secondary_animated_sprite.is_playing():
secondary_animated_sprite.play("ability_cooldown")
await get_tree().create_timer(secondary_animated_sprite.get_animation("ability_cooldown").length).timeout
secondary_animated_sprite.stop()
This way, you prevent the animation from being triggered multiple times if the ability is activated again before the cooldown animation finishes.