MCPcopy Create free account
hub / github.com/Redot-Engine/redot-engine / reparent

Method reparent

scene/main/node.cpp:2042–2086  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2040}
2041
2042void Node::reparent(Node *p_parent, bool p_keep_global_transform) {
2043 ERR_THREAD_GUARD
2044 ERR_FAIL_NULL(p_parent);
2045 ERR_FAIL_NULL_MSG(data.parent, "Node needs a parent to be reparented.");
2046 ERR_FAIL_COND_MSG(p_parent == this, vformat("Can't reparent '%s' to itself.", p_parent->get_name()));
2047
2048 if (p_parent == data.parent) {
2049 return;
2050 }
2051
2052 bool preserve_owner = data.owner && (data.owner == p_parent || data.owner->is_ancestor_of(p_parent));
2053 Node *owner_temp = data.owner;
2054 LocalVector<Node *> common_parents;
2055
2056 // If the new parent is related to the owner, find all children of the reparented node who have the same owner so that we can reassign them.
2057 if (preserve_owner) {
2058 LocalVector<Node *> to_visit;
2059
2060 to_visit.push_back(this);
2061 common_parents.push_back(this);
2062
2063 while (to_visit.size() > 0) {
2064 Node *check = to_visit[to_visit.size() - 1];
2065 to_visit.resize(to_visit.size() - 1);
2066
2067 for (int i = 0; i < check->get_child_count(false); i++) {
2068 Node *child = check->get_child(i, false);
2069 to_visit.push_back(child);
2070 if (child->data.owner == owner_temp) {
2071 common_parents.push_back(child);
2072 }
2073 }
2074 }
2075 }
2076
2077 data.parent->remove_child(this);
2078 p_parent->add_child(this);
2079
2080 // Reassign the old owner to those found nodes.
2081 if (preserve_owner) {
2082 for (Node *E : common_parents) {
2083 E->set_owner(owner_temp);
2084 }
2085 }
2086}
2087
2088Node *Node::get_parent() const {
2089 return data.parent;

Callers 10

add_expanded_editorMethod · 0.45
editMethod · 0.45
internal_processMethod · 0.45
test_viewport.hFile · 0.45
test_node.hFile · 0.45

Calls 11

vformatFunction · 0.85
is_ancestor_ofMethod · 0.80
set_ownerMethod · 0.80
sizeMethod · 0.65
get_nameMethod · 0.45
push_backMethod · 0.45
resizeMethod · 0.45
get_child_countMethod · 0.45
get_childMethod · 0.45
remove_childMethod · 0.45
add_childMethod · 0.45

Tested by

no test coverage detected