Tween restart on signal

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)

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)

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

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)

All good. :slight_smile:

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