How do you get all nodes of a certain class?

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

I need the equivalent of Object.FindObjectsOfType() from Unity, where I get every object running that class or a subclass of it. I can’t find out how to do it anywhere. I’ve found get_tree().get_nodes_in_group(), but apparently a group isn’t referring to a class because I’m getting an empty list.

:bust_in_silhouette: Reply From: sash-rc
func findByClass(node: Node, className : String, result : Array) -> void:
  if node.is_class(className) :
    result.push_back(node)
  for child in node.get_children():
    findByClass(child, className, result)


func test() -> void:
  var res = []
  findByClass(self, "Control", res)
  print(res)

I’m about 3 months new to godot so I may need correcting but I’ve learned that the function ‘is_class’ disregards class_name declarations so this wont be able to filter custom classes.

However, if you preload the class as a resource at the top of your script you can reference that instead of passing this function a sting as the middle parameter.