In our last tutorial, we learnt how to destroy a node during runtime. In this tutorial, we will include a delay timer to the script and destroy the object after the delay
The simplest way is to create a variable and subtract it with delta in the _process(). When the variable hits zero, destroy the object.
extends Spatial var object_to_delete var timer=10 func _ready(): object_to_delete=get_node("../glass cube2") func _process(delta): timer-=delta if timer<0 : if is_instance_valid(object_to_delete): print("Hello") object_to_delete.queue_free()
The second method is to use the yield statement with a timer
When the timer becomes zero it will execute the next statement.
extends Spatial var object_to_delete func _ready(): object_to_delete=get_node("../glass cube2") yield(get_tree().create_timer(10),"timeout") if is_instance_valid(object_to_delete): object_to_delete.queue_free()
The yield statement creates a delay of 10 seconds using the timer we have created inside it. You can increase or decrease the timer value by changing the integer inside the create_timer() function.
Thats it for this tutorial. If you have any questions leave it in the comment section below.