Using tween inside for loop

Godot Version

4.3

Question

Hi, I’m using tween inside a loop to animate multi-node. I want to start all node animations at the same time, but the problem is that after finishing all animation for the first node, the second node starts. If I use set_parallel(), all animation starts at the same time. So, how do I fix that?

extends Node2D

@onready var icon: Sprite2D = $Icon
@onready var icon_2: Sprite2D = $Icon2

func _ready() -> void:
	var t = create_tween()
	for node in [icon, icon_2]:
		t.tween_property(node, "position", Vector2.ZERO, 1)
		t.tween_property(node, "position", Vector2(500, 500), 1)

I want to first run t.tween_property(node, "position", Vector2.ZERO, 1) for both node then t.tween_property(node, "position", Vector2(500, 500), 1)
should I use a loop for each animation?

	for node in [icon, icon_2]:
		t.tween_property(node, "position", Vector2.ZERO, 1)
	for node in [icon, icon_2]:
		t.tween_property(node, "position", Vector2(500, 500), 1)

So you want to run a second tween as soon as the first is finished? You can use chain()

I want something like this but inside a for loop for more nodes, and more animation, and flexibility:

	var t = create_tween()
	t.tween_property(icon, "position", Vector2.ZERO, 1)
	t.parallel().tween_property(icon_2, "position", Vector2.ZERO, 1)
	t.tween_property(icon, "position", Vector2(400, 400), 1)
	t.parallel().tween_property(icon_2, "position", Vector2(400, 400), 1)
var t = create_tween()
for vector: Vector2 in [Vector2.ZERO, Vector2(400, 400)]:
    t.set_parallel(false)
    for node: Node in [icon, icon_2]:
        t.tween_property(node, "position", vector, 1)
        t.set_parallel(true)
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.