0 votes

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!

asked Nov 23, 2019 in Engine by Dumuz (100 points)

1 Answer

+1 vote
Best answer

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.

answered Nov 23, 2019 by kidscancode (13,084 points)
selected Nov 25, 2019 by Dumuz

Interesting. Thank you!

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!

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.

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.