Search code examples
godot4

How to wait for tweens in Godot 4


Previously, in Godot 3.1 I had something like this to wait for a Tween animation to finish:

await current_screen.tween.tween_completed

Where the tween was a node of the current_screen. Now, as I understand it, we are supposed to create the tween just when we need it (as the documentation suggests). This makes it so the current_screen does not have a tween node and thus no way to know if the tween created inside a function is completed or not.

How can I achieve the same behaviour as with tween_completed in Godot 4?


Solution

  • You are correct, in Godot4 we normally instantiate a Tween programmatically and handle everything in code.

    An easy way to run a process after the tween has completed is to connect to it's finished signal.

    @onready var sprite: Sprite2D = $Sprite
    
    func _ready():
        var tween = get_tree().create_tween()
        tween.tween_property(sprite, "position", Vector2(600, 600), 3)
        tween.connect("finished", on_tween_finished)
        
    func on_tween_finished():
        print("Tween done!")
    

    The on_tween_finished function will be called after the tween has been fully completed.

    Here are some of the other signals one can use when working with Tweens: https://docs.godotengine.org/en/stable/classes/class_tween.html#signals