- Edited
In my game, written in godot 3.5, I use 2 routines to get the (first) node, in a hierarchy, having a given type or type name, and they work fine even with custom classes (i.e. defined with class_name
).
They are:
static func find_node_by_type_name(type_name:String, root_node:Node):
# get the first node instance with the given type name: by using tree's
# iterative breadth search, the first ones found are returned
var instance = null
var stack = []
stack.append_array(root_node.get_children())
while not stack.is_empty():
var child = stack.pop_front()
if child.get_class() == type_name:
instance = child
break
stack.append_array(child.get_children())
return instance
static func find_node_by_type(type, root_node:Node):
# get the first node instance with the given type: by using tree's
# iterative breadth search, the first ones found are returned
var instance = null
var stack = []
stack.append_array(root_node.get_children())
while not stack.is_empty():
var child = stack.pop_front()
if child is type:
instance = child
break
stack.append_array(child.get_children())
return instance
Note that the first one,find_node_by_type_name
, works with custom types since I've overridden the two methods get_class
and is_class
for all of my custom types.
Now in godot 4.1 these functions don't work anymore, since:
get_class
andis_class
methods can no longer be overriddena type can no longer be passed as function parameter.
So, does anyone have an idea how to fix this or know of an alternative way to achieve the same results?
Thanks