I am trying to get spawned items to remove after a set amount of time. I found some questions similar to this here and attempted to modify the suggestions to make them work for my project. Currently it does not seem like the timer is ever activated because I never get the timeout printout.

extends YSort

onready var timer = get_node("/root/Node2D/Del_Timer")

func _physics_process(delta: float) -> void:
	if self.get_child_count() > 0:
		timer.start()

func _on_Del_Timer_timeout() -> void:
	print("timeout")
	var children = self.get_children()
	for c in children:
		self.remove_child(c)
		c.queue_free()

Thank you, unless I am missing something about timers I dont think thats where I am going wrong.

This is possible to do from code without the need for a timer node. Here's the most convenient way I've found to do this: (3 second timer as an example)

func _ready():
	yield(get_tree().create_timer(3.0), "timeout")
	queue_free()

Maybe try something like this:

extends YSort

onready var timer = get_node("/root/Node2D/Del_Timer")

var index = 0

func _physics_process(delta: float) -> void:
    if (self.get_child_count() > 0 && index == 0):
        timer.set_wait_time(100)  #  Sets the 'wait' time to 100 seconds
        timer.start()
        index += 1


func _on_Del_Timer_timeout() -> void:
    print("timeout")
    var children = self.get_children()
    timer.stop()
    for c in children:
        self.remove_child(c)
        c.queue_free()
    # index = 0 ## Optional -- only if you want to restart the timer immediately after freeing the children nodes

@GreyWolf117 @Azedaxen Thank you both for trying to help me out. In the end I "cheated" and went about the issue in a different way. Instead of trying to have the children removed I went to the actual instanced scene and put the queue free there with a filter to determine if it was necessary lol

Looking at the code in the OP, I think the issue is probably that the timer is being told to start continuously whenever there is at least a single child. I think this is resetting the timer before it has a chance to time out, which in turn stops it from sending a signal.

Something like this might work, since it checks to see if the timer is running before starting the timer.

func _physics_process(delta: float) -> void:
	if self.get_child_count() > 0:
		if (timer.time_left <= 0): # the timer has stopped or has finished.
			timer.start()

Though since a solution has been found, I guess it's all good anyway :smile:

@TwistedTwigleg your solution would have worked too =) that is exactly what was happening. Thank you.

2 years later