How to check if an - add child - instance node still exist

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

I know that is_instance_validcould check if a node is exist. However, as the nodes that I want to check were added to the tree by add child method while the game is running. Therefore, I don’t know how to get the node path for the is_instance_valid. So what is a good way to check if an - add child - instance node still exist in the tree? Thank you.

:bust_in_silhouette: Reply From: Zylann

If you want to check if a node exists, you need a way to identify it. Without that, the question doesn’t make sense.

You can identify a node by having a reference to it, and then use is_instance_valid(node). If your script is the one adding its child, then you may keep a reference to the node in a member variable so that it will be accessible by all functions of your script as long as your parent node exists, like so:

var child

func some_function():
	child = SomeScene.instance()

func some_other_function():
	if is_instance_valid(child):
		# ...

I have noticed that you have edited your answer from your original one, they are both very helpful and insightful, thank you for taking the time and efforts to answer my question. The method works, now my instanced scene works nicely.

Idleman | 2020-09-12 06:55

One more question, not sure what the problem is, but if I instanced a few enemies. Some times the is_instance_valid(node)returns false when there are still enemies node on the tree. Have you encounter instances like this? Thank you.

Idleman | 2020-09-13 00:21

is_instance_valid(node) will check if the node in particular still exists. It obviously does not check if other instances of the same scenes exist, it only does one at a time. A variable can only contain one reference so if you re-use it when instancing more enemies, it will only contain whatever enemy you created last.

If you want to check if any instances of the enemy scene are present in the scene, you have several options:

  • Instead of keeping one in a variable, keep them all in an array, and check for the existence of all of them using a for loop. However, it’s quite a clunky solution.

  • A better idea is to put your enemies in a group, "enemies" for example. This way, at runtime, you can get the list of present enemies with get_tree().get_nodes_in_group("enemies"), and check if it’s empty. I could not find a dedicated section of the doc, but it mentions one usage of them within a tutorial:

  • Another more complicated way is to iterate over all nodes of the scene and count the nodes that have their filename property equal to "res://path/to/enemy.tscn".

Zylann | 2020-09-13 00:32

I have tested it and you are right, the is_instance_valid(node) only check the last instanced, and that is why sometimes it works and sometime it does not. Thank for providing ways to solve the problem, I think I will try to make the enemies send a destroyed signal from the enemy script, and use the signal to trigger following actions. Thank you again for the help.

Idleman | 2020-09-13 01:50