Check if packedscene instance exists in node tree before adding it to the node tree

Godot Version

4.3

Question

I want to add an instance of a packedscene to a node tree, using the following code:

var packed_scene=load(string_path)
var instance=packed_scene.instantiate()
add_child(instance)

before adding the packed_scene instance, I want to check if an instance of the packed_scene already exists in the specific node tree and not add the child if it already exists.

Thats difficult to check, since when they are added as a child they are no longer scenes, but just nodes with scripts.

You could give your root-node of the scene a script with a class_name and then go through the whole scene-tree and check if any of the nodes is this specific class

1 Like