tween start after a condition is true

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By damianus

hello again, this time i ask you how to start a tween if a condition is true?

the structure i want to achieve is like:

tween1 (node modulate.a) appear

some stuff

if some stuff is true:

tween2 (node modulate.a) disappear
self.queue_free

:bust_in_silhouette: Reply From: exuin

Depending on your node structure, you should either call a function to start the tween, or use a signal in order to start the tween.

:bust_in_silhouette: Reply From: Whalesstate
# creating a new tween and adding it as a child
var tween = Tween.new()
add_child(tween)
# it could be any other node
var node = ColorRect.new()
add_child(node)
# white is the default modulate color , also you can use self_modulate if the node has children
var final_col = Color.white
# this is the condition which make us decide to make the alpha equal 1 [show] or 0 [hide]
var appear = false
# if appear is true the final color alpha will be equal one else it will be equal zero
final_col.a = 1.0 if appear else 0.0
# adding the required params to the tween
tween.interpolate_property(node, 'modulate', node.modulate, final_col, 2, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
# starting the tween
tween.start()
# wait until tween finishes
yield(tween, 'tween_all_completed')
# here you can check if appear == false and delete the node or the node.modulate.a == 0.0 
if !appear:
	# we remove the child but it will not be deleted
	remove_child(node)
	# we queue the node to be deleted in the next idle frame
	node.queue_free()