#How to dynamically queue up tweens?

1 messages · Page 1 of 1 (latest)

wet sable
#

I have a node that I would like to animate with tweens. The desired behaviour is:

  • When I click the node, if it is not currently playing a tween, it plays a tween immediately
  • When I click the node, if it is currently playing a tween, it appends a different tween animation to be run after the current (and all other previously queued) tweens have finished

What is the best way to achieve this? Godot does not allow for new tweeners to be added to a tween that is currently running.

loud lodge
#

You could append your tweens to an array, then have a function that gets called by the "tween_ended" signal to pop the next tween from your queue

ornate cipher
#

something like this

extends Node2D

@onready var icon := $Icon

var tween: Tween = null # current tween
var tween_queue: Array[Tween] = [] # global queue

func queue_callback():
    tween =  tween_queue.pop_front()
    if tween: tween.play()

func _input(event: InputEvent) -> void:
    if event is InputEventMouseButton and event.pressed:
        var next_tween = get_tree().create_tween()
        next_tween.tween_property(icon, "scale:x", randf_range(0., 3.), 3.)
        next_tween.tween_callback(queue_callback)
        
        if tween:
            next_tween.pause()
            tween_queue.push_back(next_tween)
        else:
            tween = next_tween