Looping Tweens - random values?

Running Godot 4.4.1

Heya, I have a Tween set up as usual, but I want its random values to re-roll every loop.
AS you see, I have a randf_range() call in there. This seems to be rolled only once, when the tween is called for the first time, and after that, the target value never changes. So: did I make a mistake here or is this expected behavior? And can I work around it?

func do_lighting() -> void:
	var light = $PointLight2D
	
	var t_light: Tween = create_tween().bind_node(spacelight).set_loops().set_parallel(true)
	t_light.tween_property(self, "progress_ratio", 1.0, 10.0)
	t_light.tween_property($PointLight2D, "energy", randf_range(1.60, 2.0), 5.0)
	t_light.chain().tween_callback(reset_light)

func reset_light() -> void:
	progress_ratio = 0.0

1 Like

That’s the expected behavior. If the tween will have different values each iteration then don’t loop it and re-create it every time it finishes.

1 Like

This does make sense, is still a bummer :smiley:

An elegant way to handle this while keeping all tweenning code in one place is to make a coroutine that re-launches the tween

func do_light(): #call me only once
	while true:
		var t: Tween = create_tween()
		t.tween_property($light, "property", randf(), 1.0)
		await t.finished

If you need to be able to cancel, simply add a class property flag that is tested as the while condition.

1 Like

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