Tween.finished not emitted from custom tween handler

Godot Version

4.2.2.stable

Question

Hi everyone,

I have some problems with my custom tween handler.
In an attempt to avoid boilerplate code for every tween I create, I thought I would write my own Utility function for this. Since I often tween from inside the process function, I end up writing this:

var tween1 : Tween
if tween:
	tween.kill()
tween = create_tween()
tween.tween_property(self, "scale", Vector2.ZERO, .25)

So I added this to my global utilities script:

var tween_dict: Dictionary = {}

func tween(target_node: Node, property: String, final_value, duration: float) -> Tween:
    var key = "%s_%s" % [target_node.get_instance_id(), property]
    
   if tween_dict.has(key):
         tween_dict[key].kill()
    
   tween_dict[key] = create_tween()
   tween_dict[key].tween_property(target_node, property, final_value, duration)
    
    tween_dict[key].finished.connect(func(): _on_tween_finished_deferred(key))
    return tween_dict[key]


func _on_tween_finished_deferred(key: String):
    call_deferred("_kill_tween_if_exists", key)


func _kill_tween_if_exists(key: String):
    if tween_dict.has(key):
        tween_dict[key].kill()
        tween_dict.erase(key)

This allows me to write the same code with 1 short line, like this:

GlobalUtilities.tween(self, "scale", Vector2.ZERO, 0.25)

And while it works as expected, I have one issue:

var tween1 : Tween
if tween1:
	tween1.kill()
tween1 = create_tween()
tween1.tween_property(self, "scale", Vector2.ZERO, .25)
await tween1.finished
print("this gets executed.")
		
var tween2: Tween = GlobalUtilities.tween(self, "scale", Vector2.ZERO, 0.25)
await tween2.finished
print("this does not get executed.")

The tween.finished signal is not emitted, and I am not sure why that is the case.
Can anyone help me out here?Preformatted text

i think your global utilities kill your tween first so the await is never finished because tween already gone

try disable this line to see, if it’s true

Hi, thanks for the reply!

I should have mentioned that I thought about this being the issue, but disabling the line results in the same problem.

i see, if you print the tween2, what does it say?

It says

<Tween#-9223371217036112117> (bound to GlobalUtilities)

I cannot reproduce your issue. Copied your code without any changes, it ran fine, both tween1 and tween2 finished and their respective prints were shown.

Can you provide an example project?