|
42 | 42 | #include "scene/main/window.h"
|
43 | 43 | #include "scene/resources/packed_scene.h"
|
44 | 44 | #include "viewport.h"
|
| 45 | +#include "modules/gdscript/gdscript_utility_functions.h" |
45 | 46 |
|
46 | 47 | #include <stdint.h>
|
47 | 48 |
|
@@ -1827,6 +1828,30 @@ bool Node::has_node(const NodePath &p_path) const {
|
1827 | 1828 | return get_node_or_null(p_path) != nullptr;
|
1828 | 1829 | }
|
1829 | 1830 |
|
| 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 | + |
1830 | 1855 | // Finds the first child node (in tree order) whose name matches the given pattern.
|
1831 | 1856 | // Can be recursive or not, and limited to owned nodes.
|
1832 | 1857 | Node *Node::find_child(const String &p_pattern, bool p_recursive, bool p_owned) const {
|
@@ -3536,6 +3561,7 @@ void Node::_bind_methods() {
|
3536 | 3561 | ClassDB::bind_method(D_METHOD("get_node", "path"), &Node::get_node);
|
3537 | 3562 | ClassDB::bind_method(D_METHOD("get_node_or_null", "path"), &Node::get_node_or_null);
|
3538 | 3563 | 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); |
3539 | 3565 | ClassDB::bind_method(D_METHOD("find_child", "pattern", "recursive", "owned"), &Node::find_child, DEFVAL(true), DEFVAL(true));
|
3540 | 3566 | ClassDB::bind_method(D_METHOD("find_children", "pattern", "type", "recursive", "owned"), &Node::find_children, DEFVAL(""), DEFVAL(true), DEFVAL(true));
|
3541 | 3567 | 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)
|
3973 | 3999 | return Object::is_connected(p_signal, p_callable);
|
3974 | 4000 | }
|
3975 | 4001 |
|
| 4002 | + |
3976 | 4003 | #endif
|
0 commit comments