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