Skip to content

Commit

Permalink
Implement Node.clear_children
Browse files Browse the repository at this point in the history
  • Loading branch information
RadiantUwU committed Dec 30, 2024
1 parent eb51030 commit 511dcae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions doc/classes/Node.xml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@
If the node is not inside the tree, returns [code]false[/code] no matter the value of [member process_mode].
</description>
</method>
<method name="clear_children">
<return type="void" />
<param index="0" name="deferred" type="bool" default="true" />
<param index="1" name="include_internal" type="bool" default="false" />
<description>
Calls [method queue_free] on all children.
If [param deferred] is [code]false[/code], [method remove_child] is called before [method queue_free].
If [param include_internal] is [code]false[/code], excludes internal children from being cleared (see [method add_child]'s [code]internal[/code] parameter).
</description>
</method>
<method name="create_tween">
<return type="Tween" />
<description>
Expand Down
15 changes: 15 additions & 0 deletions scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,20 @@ void Node::remove_child(Node *p_child) {
}
}

void Node::clear_children(bool p_deferred, bool p_include_internal) {
if (p_deferred) {
for (int i = 0; i < get_child_count(p_include_internal); i++) {
get_child(i, p_include_internal)->queue_free();
}
} else {
while (get_child_count(p_include_internal)) {
Node *n = get_child(0, p_include_internal);
remove_child(n);
n->queue_free();
}
}
}

void Node::_update_children_cache_impl() const {
// Assign children
data.children_cache.resize(data.children.size());
Expand Down Expand Up @@ -3599,6 +3613,7 @@ void Node::_bind_methods() {
ClassDB::bind_method(D_METHOD("find_parent", "pattern"), &Node::find_parent);
ClassDB::bind_method(D_METHOD("has_node_and_resource", "path"), &Node::has_node_and_resource);
ClassDB::bind_method(D_METHOD("get_node_and_resource", "path"), &Node::_get_node_and_resource);
ClassDB::bind_method(D_METHOD("clear_children", "deferred", "include_internal"), &Node::clear_children, DEFVAL(true), DEFVAL(false));

ClassDB::bind_method(D_METHOD("is_inside_tree"), &Node::is_inside_tree);
ClassDB::bind_method(D_METHOD("is_part_of_edited_scene"), &Node::is_part_of_edited_scene);
Expand Down
1 change: 1 addition & 0 deletions scene/main/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ class Node : public Object {
void add_child(Node *p_child, bool p_force_readable_name = false, InternalMode p_internal = INTERNAL_MODE_DISABLED);
void add_sibling(Node *p_sibling, bool p_force_readable_name = false);
void remove_child(Node *p_child);
void clear_children(bool p_deferred = true, bool p_include_internal = true);

int get_child_count(bool p_include_internal = true) const;
Node *get_child(int p_index, bool p_include_internal = true) const;
Expand Down

0 comments on commit 511dcae

Please sign in to comment.