fix node duplication in update after external changes.

This commit is contained in:
ajreckof
2024-03-28 20:26:44 +01:00
parent 7d151c8381
commit ae472865d0
7 changed files with 95 additions and 45 deletions

View File

@@ -29,6 +29,7 @@
/**************************************************************************/
#include "node.h"
#include "node.compat.inc"
#include "core/config/project_settings.h"
#include "core/core_string_names.h"
@@ -3007,7 +3008,7 @@ static void find_owned_by(Node *p_by, Node *p_node, List<Node *> *p_owned) {
}
}
void Node::replace_by(Node *p_node, bool p_keep_groups) {
void Node::replace_by(Node *p_node, bool p_keep_groups, bool p_keep_children) {
ERR_THREAD_GUARD
ERR_FAIL_NULL(p_node);
ERR_FAIL_COND(p_node->data.parent);
@@ -3028,13 +3029,13 @@ void Node::replace_by(Node *p_node, bool p_keep_groups) {
_replace_connections_target(p_node);
if (data.owner) {
for (int i = 0; i < get_child_count(); i++) {
find_owned_by(data.owner, get_child(i), &owned_by_owner);
if (p_keep_children) {
for (int i = 0; i < get_child_count(); i++) {
find_owned_by(data.owner, get_child(i), &owned_by_owner);
}
}
_clean_up_owner();
}
Node *parent = data.parent;
int index_in_parent = get_index(false);
@@ -3046,31 +3047,33 @@ void Node::replace_by(Node *p_node, bool p_keep_groups) {
emit_signal(SNAME("replacing_by"), p_node);
while (get_child_count()) {
Node *child = get_child(0);
remove_child(child);
if (!child->is_owned_by_parent()) {
// add the custom children to the p_node
Node *child_owner = child->get_owner() == this ? p_node : child->get_owner();
child->set_owner(nullptr);
p_node->add_child(child);
child->set_owner(child_owner);
if (p_keep_children) {
while (get_child_count()) {
Node *child = get_child(0);
remove_child(child);
if (!child->is_owned_by_parent()) {
// add the custom children to the p_node
Node *child_owner = child->get_owner() == this ? p_node : child->get_owner();
child->set_owner(nullptr);
p_node->add_child(child);
child->set_owner(child_owner);
}
}
for (Node *E : owned) {
if (E->data.owner != p_node) {
E->set_owner(p_node);
}
}
for (Node *E : owned_by_owner) {
if (E->data.owner != owner) {
E->set_owner(owner);
}
}
}
p_node->set_owner(owner);
for (Node *E : owned) {
if (E->data.owner != p_node) {
E->set_owner(p_node);
}
}
for (Node *E : owned_by_owner) {
if (E->data.owner != owner) {
E->set_owner(owner);
}
}
p_node->set_scene_file_path(get_scene_file_path());
}
@@ -3597,7 +3600,7 @@ void Node::_bind_methods() {
ClassDB::bind_method(D_METHOD("create_tween"), &Node::create_tween);
ClassDB::bind_method(D_METHOD("duplicate", "flags"), &Node::duplicate, DEFVAL(DUPLICATE_USE_INSTANTIATION | DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS));
ClassDB::bind_method(D_METHOD("replace_by", "node", "keep_groups"), &Node::replace_by, DEFVAL(false));
ClassDB::bind_method(D_METHOD("replace_by", "node", "keep_groups", "keep_children"), &Node::replace_by, DEFVAL(false), DEFVAL(true));
ClassDB::bind_method(D_METHOD("set_scene_instance_load_placeholder", "load_placeholder"), &Node::set_scene_instance_load_placeholder);
ClassDB::bind_method(D_METHOD("get_scene_instance_load_placeholder"), &Node::get_scene_instance_load_placeholder);