diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2023-01-07 13:20:56 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2023-01-07 13:20:56 +0100 |
commit | 3579d7a9f74e3f1eda88826897feb4b866d75ec9 (patch) | |
tree | 40afa501f94493b7df9dc41c6d7b3ed738f0b15d /scene/main | |
parent | 447aa5b0259ab18139d86297dfccb8dd68ef2e30 (diff) | |
parent | 2bdd7e9ea0383c06fe6a83445469e41222477abc (diff) |
Merge pull request #36301 from KoBeWi/daddy_node
Add reparent methods to Node
Diffstat (limited to 'scene/main')
-rw-r--r-- | scene/main/node.cpp | 13 | ||||
-rw-r--r-- | scene/main/node.h | 1 |
2 files changed, 14 insertions, 0 deletions
diff --git a/scene/main/node.cpp b/scene/main/node.cpp index 8f173e4c0d..3544ec4c83 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -1439,6 +1439,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; } @@ -2784,6 +2796,7 @@ void Node::_bind_methods() { ClassDB::bind_method(D_METHOD("get_name"), &Node::get_name); ClassDB::bind_method(D_METHOD("add_child", "node", "force_readable_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)); diff --git a/scene/main/node.h b/scene/main/node.h index 611f48c400..398465c3cd 100644 --- a/scene/main/node.h +++ b/scene/main/node.h @@ -317,6 +317,7 @@ public: bool has_node_and_resource(const NodePath &p_path) const; Node *get_node_and_resource(const NodePath &p_path, Ref<Resource> &r_res, Vector<StringName> &r_leftover_subpath, bool p_last_is_property = true) const; + virtual void reparent(Node *p_parent, bool p_keep_global_transform = true); Node *get_parent() const; Node *find_parent(const String &p_pattern) const; |