Skip to content

Commit f341ae7

Browse files
committed
find_child_of_type
1 parent e3550cb commit f341ae7

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

modules/gdscript/gdscript_utility_functions.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,21 @@ struct GDScriptUtilityFunctionsDefinitions {
609609
}
610610
};
611611

612+
// expose is_instance_of to C++
613+
bool GDScriptUtilityFunctions::is_instance_of(const Variant &p_value, const Variant &p_type) {
614+
Variant ret;
615+
const Variant *args[2] = { &p_value, &p_type };
616+
Callable::CallError error;
617+
GDScriptUtilityFunctionsDefinitions::is_instance_of(&ret, args, 2, error);
618+
619+
// if there was an error, return false
620+
if (error.error != Callable::CallError::CALL_OK) {
621+
return false;
622+
}
623+
624+
return ret;
625+
}
626+
612627
struct GDScriptUtilityFunctionInfo {
613628
GDScriptUtilityFunctions::FunctionPtr function = nullptr;
614629
MethodInfo info;

modules/gdscript/gdscript_utility_functions.h

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class GDScriptUtilityFunctions {
5656

5757
static void register_functions();
5858
static void unregister_functions();
59+
static bool is_instance_of(const Variant &p_value, const Variant &p_type);
5960
};
6061

6162
#endif // GDSCRIPT_UTILITY_FUNCTIONS_H

scene/main/node.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "scene/main/window.h"
4343
#include "scene/resources/packed_scene.h"
4444
#include "viewport.h"
45+
#include "modules/gdscript/gdscript_utility_functions.h"
4546

4647
#include <stdint.h>
4748

@@ -1827,6 +1828,30 @@ bool Node::has_node(const NodePath &p_path) const {
18271828
return get_node_or_null(p_path) != nullptr;
18281829
}
18291830

1831+
1832+
Node *Node::find_child_of_type(const Variant &p_type) const {
1833+
ERR_THREAD_GUARD_V(nullptr);
1834+
_update_children_cache();
1835+
Node *const *cptr = data.children_cache.ptr();
1836+
int ccount = data.children_cache.size();
1837+
for (int i = 0; i < ccount; i++) {
1838+
Node* node = cptr[i];
1839+
1840+
// if p_type is a string, check if the node is an instance of the class
1841+
if (p_type.get_type() == Variant::STRING || p_type.get_type() == Variant::STRING_NAME) {
1842+
if (node->is_class(p_type)) {
1843+
return node;
1844+
}
1845+
} else {
1846+
// if p_type is a class, check if the node is an instance of the class
1847+
if (GDScriptUtilityFunctions::is_instance_of(node, p_type)) {
1848+
return node;
1849+
}
1850+
}
1851+
}
1852+
return nullptr;
1853+
}
1854+
18301855
// Finds the first child node (in tree order) whose name matches the given pattern.
18311856
// Can be recursive or not, and limited to owned nodes.
18321857
Node *Node::find_child(const String &p_pattern, bool p_recursive, bool p_owned) const {
@@ -3536,6 +3561,7 @@ void Node::_bind_methods() {
35363561
ClassDB::bind_method(D_METHOD("get_node", "path"), &Node::get_node);
35373562
ClassDB::bind_method(D_METHOD("get_node_or_null", "path"), &Node::get_node_or_null);
35383563
ClassDB::bind_method(D_METHOD("get_parent"), &Node::get_parent);
3564+
ClassDB::bind_method(D_METHOD("find_child_of_type", "type"), &Node::find_child_of_type);
35393565
ClassDB::bind_method(D_METHOD("find_child", "pattern", "recursive", "owned"), &Node::find_child, DEFVAL(true), DEFVAL(true));
35403566
ClassDB::bind_method(D_METHOD("find_children", "pattern", "type", "recursive", "owned"), &Node::find_children, DEFVAL(""), DEFVAL(true), DEFVAL(true));
35413567
ClassDB::bind_method(D_METHOD("find_parent", "pattern"), &Node::find_parent);
@@ -3973,4 +3999,5 @@ bool Node::is_connected(const StringName &p_signal, const Callable &p_callable)
39733999
return Object::is_connected(p_signal, p_callable);
39744000
}
39754001

4002+
39764003
#endif

scene/main/node.h

+1
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ class Node : public Object {
455455
bool has_node(const NodePath &p_path) const;
456456
Node *get_node(const NodePath &p_path) const;
457457
Node *get_node_or_null(const NodePath &p_path) const;
458+
Node *find_child_of_type(const Variant &p_type) const;
458459
Node *find_child(const String &p_pattern, bool p_recursive = true, bool p_owned = true) const;
459460
TypedArray<Node> find_children(const String &p_pattern, const String &p_type = "", bool p_recursive = true, bool p_owned = true) const;
460461
bool has_node_and_resource(const NodePath &p_path) const;

0 commit comments

Comments
 (0)