Why isnt this tween with yield repeating?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By indy2005

So I thought this would work, but it expands one and contracts once, but then the second yield never gets triggered when I though this would be a way to get the node to grow and shrink repeatedly. It’s almost as if, once a tween has fired an on_completed event, it never fires it again, even if you set it to start() again?

var tweenExpand = Tween.new()
var tweenContract = Tween.new()


# Called when the node enters the scene tree for the first time.
func _ready():
	
	add_child(tweenExpand)
	add_child(tweenContract)
	tweenExpand.interpolate_property(self, "scale", null, Vector2(2.0,2.0),2,Tween.TRANS_LINEAR)
	tweenContract.interpolate_property(self, "scale", Vector2(2.0,2.0), Vector2(1.0,1.0),2,Tween.TRANS_LINEAR)
	applyTweens()
	

func applyTweens():
	while true:
		
		
		tweenExpand.start()
		yield(tweenExpand,"tween_completed")
		
		tweenContract.start()
		yield(tweenContract,"tween_completed")
:bust_in_silhouette: Reply From: Thomas Karcher

As you already noted, a completed Tween will not restart without calling the interpolate method again first. Once you do that, a loop can easily be implemented as shown here: https://forum.godotengine.org/24585/how-to-loop-a-tween-solved

Why isnt this working then?

func applyTweens():
	while true:
		print("Tween Starting")
		tweenExpand.interpolate_property(self, "scale",  Vector2(1.0,1.0), Vector2(2.0,2.0),2,Tween.TRANS_LINEAR)
		tweenExpand.start()
		yield(tweenExpand,"tween_completed")
		print("tweenExpand Ended")
		
		tweenContract.interpolate_property(self, "scale", Vector2(2.0,2.0), Vector2(1.0,1.0),2,Tween.TRANS_LINEAR)
		tweenContract.start()
		yield(tweenContract,"tween_completed")
		print("tweenContract Ended")

This just generates this output then stops. It expands, contracts, expands then stops…

Tween Starting
tweenExpand Ended
tweenContract Ended
Tween Starting
tweenExpand Ended

Where as if I just use one tween instance to do the expansion and contraction it works, but I don’t understand why.

func applyTweens():
	while true:
		print("Tween Starting")
		tweenExpand.interpolate_property(self, "scale",  Vector2(1.0,1.0), Vector2(2.0,2.0),2,Tween.TRANS_LINEAR)
		tweenExpand.start()
		yield(tweenExpand,"tween_completed")
		tweenExpand.interpolate_property(self, "scale",  Vector2(2.0,2.0),  Vector2(1.0,1.0), 2,Tween.TRANS_LINEAR)
		tweenExpand.start()
		yield(tweenExpand,"tween_completed")

indy2005 | 2020-09-16 21:15

I just tried your first script and it works just fine for me (Godot 3.2.1). Is anything else happening in your code which could affect / remove tweenContract during execution?

Thomas Karcher | 2020-09-17 06:33

No that I am aware of. I am instancing these nodes, not sure if that makes a difference.

indy2005 | 2020-09-17 08:08

Could you upload your project (or an excerpt of it) somewhere?

Thomas Karcher | 2020-09-17 13:44