Find out which object a tweens animation just finished on

Godot Version

4.4.1

Question

TLDR:

The finished signal of the Tween class does not have any values on it.
I need to now what Object that Tween animated on.
I found a cursed solution and am looking for a better one.

Context

I tried like this:

## A Tween that has a finished signal that communicates the object whichs tween was finished.
## Only supports one tween
class_name EUICustomTween
extends Tween

signal finished_on(target: Object)

var _tweened_object: Object

func custon_tween_property(object: Object, property: NodePath, final_val: Variant, duration: float):
	_tweened_object = object
	var new_tweener: PropertyTweener = tween_property(object, property, final_val, duration)
	new_tweener.finished.connect(_on_finished)

func _on_finished():
	finished_on.emit(_tweened_object)

But I couldnt figure out how to use this EUICustomTween because get_tree().create_tween()
and get_node().create_tween() dont exists for my custom tween.

I kind of found a workaround doing this:

## A Tween creator that has a finished signal that communicates the object whichs tween was finished.
## Only supports one tween
class_name EUICustomTween
extends Node

signal finished_on(target: Object)

var _tweened_object: Object
var new_tween: Tween

func _ready() -> void:
	new_tween = get_tree().create_tween()

func custom_tween_property(object: Object, property: NodePath, final_val: Variant, duration: float) -> PropertyTweener:
	_tweened_object = object
	var new_tweener: PropertyTweener = new_tween.tween_property(object, property, final_val, duration)
	new_tweener.finished.connect(_on_finished)
	return new_tweener

func _on_finished():
	finished_on.emit(_tweened_object)
	queue_free()

But while this does work it feels very wrong and unintended.
Anybody got better solution?
thx (-:

You can bind parameters to a callable

new_tweener.finished.connect(my_function.bind(object))

func my_function(finished_object: Object) -> void:
    print("my object: ", finished_object)
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.