Is it possible to delete instanced nodes after a timer has gone off?

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

In the game I’m making I need a sprite to despawn after a timer has gone off. The issue is, The sprite is instanced, and the node that instanced it would be queue_free()'d at this point.

Let me explain this more clearly: After the health of an enemy teachers 0, I call the queue_free() function and spawn a sprite in its place, as the child of the previous node’s owner. Now, this is all fine and dandy, but I want this sprite gone after 5 seconds, which isn’t happening.

I’ve tried a few things: I’ve tried attaching a timer to the sprite, which would queue_free() the sprite after its time has run out (the script is attached to the sprite itself). I thought about autoloading a timer that would queue_free() anything with the tag “debris” but I don’t know how I would specify which bodies the timer would target.

I’ve been thinking and searching for a solution for over an hour but I’ve come up on nothing. Any help would be appreciated.

:bust_in_silhouette: Reply From: dancaer69

So, the problem is that you cannot have access to the temporary sprite’ s instance? If this is the case, maybe you can define a variable and store there the sprite’ s instance. Then you can use this variable with a timer on its timeout method. Something like this:

var temp_sprite

temp_sprite = sprite.instance()

func on_timer_timeout():
    temp_sprite.queue_free()

I want to be able to queue_free() more than just one sprite: if two enemies die 5 seconds apart, I want their sprites to be deleted according to their respective timers (aka 5 seconds apart). To clarify I’ve set the timer to autostart and connected the timeout signal.

OiKeTTLe | 2021-04-24 13:33