1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

Merge pull request #75459 from kleonc/node-fix-find-children

Fix recursive `Node.find_children`
This commit is contained in:
Rémi Verschelde
2023-06-19 22:33:46 +02:00
2 changed files with 27 additions and 16 deletions

View File

@@ -1665,15 +1665,8 @@ TypedArray<Node> Node::find_children(const String &p_pattern, const String &p_ty
continue; continue;
} }
if (!p_pattern.is_empty()) { if (p_pattern.is_empty() || cptr[i]->data.name.operator String().match(p_pattern)) {
if (!cptr[i]->data.name.operator String().match(p_pattern)) { if (p_type.is_empty() || cptr[i]->is_class(p_type)) {
continue;
} else if (p_type.is_empty()) {
ret.append(cptr[i]);
}
}
if (cptr[i]->is_class(p_type)) {
ret.append(cptr[i]); ret.append(cptr[i]);
} else if (cptr[i]->get_script_instance()) { } else if (cptr[i]->get_script_instance()) {
Ref<Script> scr = cptr[i]->get_script_instance()->get_script(); Ref<Script> scr = cptr[i]->get_script_instance()->get_script();
@@ -1686,6 +1679,7 @@ TypedArray<Node> Node::find_children(const String &p_pattern, const String &p_ty
scr = scr->get_base_script(); scr = scr->get_base_script();
} }
} }
}
if (p_recursive) { if (p_recursive) {
ret.append_array(cptr[i]->find_children(p_pattern, p_type, true, p_owned)); ret.append_array(cptr[i]->find_children(p_pattern, p_type, true, p_owned));

View File

@@ -308,6 +308,9 @@ TEST_CASE("[SceneTree][Node] Testing node operations with a more complex simple
Node *child = SceneTree::get_singleton()->get_root()->find_child("NestedNode", true, false); Node *child = SceneTree::get_singleton()->get_root()->find_child("NestedNode", true, false);
CHECK_EQ(child, nullptr); CHECK_EQ(child, nullptr);
TypedArray<Node> children = SceneTree::get_singleton()->get_root()->find_children("NestedNode", "", true, false);
CHECK_EQ(children.size(), 0);
node1->set_name("Node1"); node1->set_name("Node1");
node2->set_name("Node2"); node2->set_name("Node2");
node1_1->set_name("NestedNode"); node1_1->set_name("NestedNode");
@@ -315,15 +318,29 @@ TEST_CASE("[SceneTree][Node] Testing node operations with a more complex simple
child = SceneTree::get_singleton()->get_root()->find_child("NestedNode", true, false); child = SceneTree::get_singleton()->get_root()->find_child("NestedNode", true, false);
CHECK_EQ(child, node1_1); CHECK_EQ(child, node1_1);
children = SceneTree::get_singleton()->get_root()->find_children("NestedNode", "", true, false);
CHECK_EQ(children.size(), 1);
CHECK_EQ(Object::cast_to<Node>(children[0]), node1_1);
// First node that matches with the name is node1. // First node that matches with the name is node1.
child = SceneTree::get_singleton()->get_root()->find_child("Node?", true, false); child = SceneTree::get_singleton()->get_root()->find_child("Node?", true, false);
CHECK_EQ(child, node1); CHECK_EQ(child, node1);
children = SceneTree::get_singleton()->get_root()->find_children("Node?", "", true, false);
CHECK_EQ(children.size(), 2);
CHECK_EQ(Object::cast_to<Node>(children[0]), node1);
CHECK_EQ(Object::cast_to<Node>(children[1]), node2);
SceneTree::get_singleton()->get_root()->move_child(node2, 0); SceneTree::get_singleton()->get_root()->move_child(node2, 0);
// It should be node2, as it is now the first one in the tree. // It should be node2, as it is now the first one in the tree.
child = SceneTree::get_singleton()->get_root()->find_child("Node?", true, false); child = SceneTree::get_singleton()->get_root()->find_child("Node?", true, false);
CHECK_EQ(child, node2); CHECK_EQ(child, node2);
children = SceneTree::get_singleton()->get_root()->find_children("Node?", "", true, false);
CHECK_EQ(children.size(), 2);
CHECK_EQ(Object::cast_to<Node>(children[0]), node2);
CHECK_EQ(Object::cast_to<Node>(children[1]), node1);
} }
SUBCASE("Nodes should be accessible via their node path") { SUBCASE("Nodes should be accessible via their node path") {