|Topic was automatically imported from the old Question2Answer platform.
I want to get a list of every instance using a particular script. Is there an API call I can use for this?
Context: I have a manager like object and I want to get every node of a certain type in order to execute some behavior on each of these nodes.
Previous approach and issues: At first I was setting each instance of this node as a child of the manager but it became messy, I would like to group these instances more intuitively. Moreover the manager should be independent of the instances so I want already existing instances to be found by it.
I am new to Godot so I am open to alternate solutions if they are cleaner.
For your manager class you should take a look at Singletons (Autoload), they are made almost exactly for that kind of job.
As for getting all nodes of a certain type, do you mean getting all nodes in a running scene?, if that’s the case, and as is explained in the link provided, you can, in your Singleton script, get the parent node of the running scene with:
var current_scene = null
var root = get_tree().get_root()
current_scene = root.get_child(root.get_child_count() - 1)
and from there iterate through the children and check with
get_name() if is the node you are looking for.
hope that helps.
Sorry for the late response, I did not get any notifications about an answer and forgot about the question.
Regarding Singletons: I was unsure about them since they seem to be accessible from every scene and I did not want for example a UI scene to have access to this object.
Regarding get node I was thinking of a traversal over the whole node tree. I was considering maintaining a list with a reference to every object but then I decided to use get the nodes dynamically and change it if it gives me performance issues. I know it is easy to implement a traversal I just wanted see if there is one given before I add the code.
J-Camilleri | 2019-05-27 16:35