On_tween_complete parameters

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

The tween_completed node creates a _On_tween_complete() with 2 parameters: Object and Key.

The function seems to work just fine without altering the parameters, but I’m curious;
How would I use these parameters?

Sorry, I’m new to coding.

Thanks!

:bust_in_silhouette: Reply From: kidscancode

tween_completed is a signal that is emitted by the Tween node. This signal is emitted when an individual interpolation of the tween finishes. Because a Tween can animate an arbitrary number of objects and properties, they can each finish at a different time.

Here’s an example that may illustrate this. Let’s assume that in our scene we have an Area2D with an attached Sprite, along with a Tween. First, we set up two interpolations:

func _ready():
	$Tween.interpolate_property(self, "position", position,
            position+Vector2(100, 0), 1.0,
            Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
	$Tween.interpolate_property($Sprite, "modulate:a", 1.0,
			0.0, 5.0, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
	$Tween.start()

The first interpolation will move the root Area2D’s position by 100 pixels over 1 second. The second will fade the Sprite’s modulate:a (alpha channel) to 0 over 5 seconds.

If we then connect the tween_completed signal, and add this:

func _on_Tween_tween_completed(object, key):
	print(object, key)

We’ll get two things printed:

[Area2D:1170]:position
[Sprite:1207]:modulate:a

The function gets called twice - once for the end of each of the interpolations. If you wanted some code to run at the time one of them ended, you could use those parameters to detect that.

Interesting. Thank you!

Dumuz | 2019-11-25 22:55

I have a follow-up question to this.

How can I reference the object variable in _on_Tween_tween_completed(object, key) if what is being generated is the I.D. and object type?

For example; say I wanted something to happen in the _on_Tween_tween_completed(object, key) only if one a specific tween finishes, out of multiple. Something like this doesn’t seem to work:

_func _on_Tween_tween_completed(object, key):
     if object == $sprite:
           #do something

So how do I use the ID and node type?

Thanks!

Dumuz | 2019-12-14 17:10

object in the signal parameters is a reference to the object the tween is operating on.

I can’t see your tween setup, but you may be accessing the wrong node. Try putting print(object.name) there to check.

kidscancode | 2019-12-14 17:30

I’m so sorry, I never got an alert to your replying.

I retested it and the object reference works exactly as expected, I was wrong in my initial question. Thank you for the quick reply you gave me though!

Dumuz | 2019-12-19 17:58