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.


in Engine by (359 points)

1 Answer

+3 votes
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)

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:


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.

by (21,718 points)
selected by

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?


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!

How to use key in on_tween_completed signal
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.