tdev
1
Godot Version
4.2
Question
How can i restart a tween whenever a signal is emitted?
code:
func _on_signal_received(value):
var label = get_node("Label")
label.text = value
label.position = Vector2(0.0,0.0)
label.visible = true
var tween = get_tree().create_tween()
tween.tween_property(label,"position",Vector2(0.0,-50.0),1)
tween.tween_property(label,"visible",false,1)
exomat
2
Reference your tween outside of your _on_signal_reseived(value)
function and kill your old tween:
var tween : Tween
func _on_signal_received(value):
if tween:
tween.kill()
var label = get_node("Label")
label.text = value
label.position = Vector2(0.0,0.0)
label.visible = true
tween = get_tree().create_tween()
tween.tween_property(label,"position",Vector2(0.0,-50.0),1)
tween.tween_property(label,"visible",false,1)
tdev
3
Error
Cannot call method 'kill' on a null value.
That’s because there’s no tween created yet when you try to kill it the first time.
func _ready():
tween = get_tree().create_tween()
or add an check:
If tween:
tween.kill()
1 Like
exomat
5
You could also use a Tween
node in your scene tree:
@export var tween : Tween
func _on_signal_received(value):
if not tween:
printerr(self.name + ": exported tween node not assigned")
return
tween.kill()
var label = get_node("Label")
label.text = value
label.position = Vector2(0.0,0.0)
label.visible = true
tween = get_tree().create_tween()
tween.tween_property(label,"position",Vector2(0.0,-50.0),1)
tween.tween_property(label,"visible",false,1)
1 Like
Did I just stole a solution check mark? Hopefully they are not that important. (srry exomat. May your deeds be rewarded in Valhala)
system
Closed
8
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.