Removing one shot timer

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

Suppose whenever I cast a skill, I create a one-shot timer with wait time 1 in script. Then it will stop after 1 second. But will it queue_free itself? I was reading the documentation and it doesn’t say anything about what happens to the timer after expiration so I assume it will just be stopped, and remain in my scene. But if that’s the case will there be a problem if I create too many timers? What is a good way to clean up all these expired one-shot timers? Thank you.

:bust_in_silhouette: Reply From: deaton64

Hi,
There’s few signals you can use on timers. Such as:

func _on_Timer_tree_entered() -> void:
	print("Tree entered")

func _on_Timer_ready() -> void:
	print("Timer ready")

func _on_Timer_timeout() -> void:
	print("Timer done")
	$Timer.queue_free()

func _on_Timer_tree_exiting() -> void:
	print("Tree exiting")

func _on_Timer_tree_exited() -> void:
	print("Tree exited")

Once the queue_free() is executed the timer leaves the scene. You can see this by running your code and clicking on the Remote nodes to view the running nodes.
If you don’t free it up, it will hang around.

Yes, thanks for the info! I was thinking about this from a design perspective, and I realized I should really make my skill a scene, and once the timer expires, queue_free() the scene itself.

qtren | 2021-04-17 16:53