How to yield until after queue_free() is done

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

I’m unloading a level and loading a new level, and the _ready() function is accessing nodes from groups in the old level, causing unintended behaviour.

It seems like it should be doable to simply yield until the old level is gone to spawn the new level, but I don’t know what signal to yield on. Any suggestions or alternative solutions are welcome!

:bust_in_silhouette: Reply From: Asthmar

Instead of Yield I would write a function that checks for the old scene in the tree and if it doesn’t have it, run your code for the new scene.

Do you mean something like this:

node_to_delete.queue_free()
while get_tree().get_root().find_node("node_name") != null:
   pass
# do what you were waiting to do

It seems more processor intensive than yielding until the correct moment?

CliffRaven | 2023-02-23 23:58

:bust_in_silhouette: Reply From: CliffRaven

What ended up working for me:

node_to_delete.queue_free()
yield(node_to_delete, "tree_exited")

In Godot 4:
yield has been replaced with await

node.queue_free()
await node.tree_exited
1 Like