I need an easy way to check which ones are still in the scene tree

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

Something that needs little code (or none) and is efficient. While this could be a new feature for the Godot Engine, because in large games needing many nodes it would be very useful to avoid losing the nodes.

:bust_in_silhouette: Reply From: p7f

You can use get_children() of the root node, which returns an array with all children in the node. You can then iterate through all of them and do what you want with them.


:bust_in_silhouette: Reply From: Xrayez

You can add nodes to a group that you think are important to check, either via code or editor. Then you can get all nodes in that group with:

for node in get_nodes_in_group('that_are_important'):
    if node.is_inside_tree():
        print("Phew, it's still alive!")

Actually I’ve come up with a utility function that can check the existence of a node in various ways:

static func node_exists(node):

    if is_instance_valid(node) and node != null and \
        node is Node and node.is_inside_tree():

	    return true

    return false