Add methods for node reparenting

This commit is contained in:
Tomasz Chabora 2020-02-17 18:29:14 +01:00 committed by kobewi
parent d5d22ab035
commit 2bdd7e9ea0
9 changed files with 50 additions and 0 deletions

View file

@ -1435,6 +1435,18 @@ TypedArray<Node> Node::find_children(const String &p_pattern, const String &p_ty
return ret;
}
void Node::reparent(Node *p_parent, bool p_keep_global_transform) {
ERR_FAIL_NULL(p_parent);
ERR_FAIL_NULL_MSG(data.parent, "Node needs a parent to be reparented.");
if (p_parent == data.parent) {
return;
}
data.parent->remove_child(this);
p_parent->add_child(this);
}
Node *Node::get_parent() const {
return data.parent;
}
@ -2792,6 +2804,7 @@ void Node::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_name"), &Node::get_name);
ClassDB::bind_method(D_METHOD("add_child", "node", "legible_unique_name", "internal"), &Node::add_child, DEFVAL(false), DEFVAL(0));
ClassDB::bind_method(D_METHOD("remove_child", "node"), &Node::remove_child);
ClassDB::bind_method(D_METHOD("reparent", "new_parent", "keep_global_transform"), &Node::reparent, DEFVAL(true));
ClassDB::bind_method(D_METHOD("get_child_count", "include_internal"), &Node::get_child_count, DEFVAL(false)); // Note that the default value bound for include_internal is false, while the method is declared with true. This is because internal nodes are irrelevant for GDSCript.
ClassDB::bind_method(D_METHOD("get_children", "include_internal"), &Node::_get_children, DEFVAL(false));
ClassDB::bind_method(D_METHOD("get_child", "idx", "include_internal"), &Node::get_child, DEFVAL(false));