Issues with Tweens

Godot Version

4.3.1

Question

Hi everyone,

I’m trying to make a simple repeating function using tweens but I keep running into an issue with not being able to start them after they have been stopped.

Here is my function:

var tween = null

func doAnimations(depth):
        # move node downwards
	node.global_position = node_original_position
	var lower_pos = node.global_position - 10
	if tween == null:
		tween = get_tree().create_tween()
		tween.tween_property(node, "position", lower_pos, 3)
		tween.tween_callback(doAnimations.bind(depth-1))
	if depth <= 0:
		tween = null
		node.global_position = node_original_position
		finishAnimation()
	else:
		tween.stop()
		tween.play()

It creates a tween, moves the node downwards and then recalls the scene with depth-1. Once depth reaches 0, it calls another method, otherwise it restarts the tween.

My issue is that tween.stop() and tween.play() is not working, and the tween is not resetting and playing.

Anyone know what I’m doing wrong?

Thanks

The tween might be restarting, it’s already at it’s lower_pos though. Maybe using .as_relative() would help?

Seems like a strange flow, why would you want to restart the tween after the first depth? Could you make it loop instead of calling the function over and over? or even set the final position mathematically as depth * -10?

var tween: Tween = null

func doAnimations(depth: int) -> void:
	if tween == null:
		tween = node.create_tween()
		tween.tween_property(node, "position:y", -10, 3).as_relative()
		tween.set_loops(depth) # using loops to repeat tween

		await tween.finished
		finishAnimation()