Skip to content

Commit ae47286

Browse files
committed
fix node duplication in update after external changes.
1 parent 7d151c8 commit ae47286

File tree

7 files changed

+92
-42
lines changed

7 files changed

+92
-42
lines changed

doc/classes/Node.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,9 @@
795795
<return type="void" />
796796
<param index="0" name="node" type="Node" />
797797
<param index="1" name="keep_groups" type="bool" default="false" />
798+
<param index="2" name="keep_children" type="bool" default="true" />
798799
<description>
799-
Replaces this node by the given [param node]. All children of this node are moved to [param node].
800+
Replaces this node by the given [param node]. If [param keep_children] is [code]true[/code] all children of this node are moved to [param node].
800801
If [param keep_groups] is [code]true[/code], the [param node] is added to the same groups that the replaced node is in (see [method add_to_group]).
801802
[b]Warning:[/b] The replaced node is removed from the tree, but it is [b]not[/b] deleted. To prevent memory leaks, store a reference to the node in a variable, or use [method Object.free].
802803
</description>

editor/editor_data.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -722,15 +722,8 @@ bool EditorData::check_and_update_scene(int p_idx) {
722722

723723
new_scene->set_scene_file_path(edited_scene[p_idx].root->get_scene_file_path());
724724
Node *old_root = edited_scene[p_idx].root;
725-
for (int i = 0; i < old_root->get_child_count(); i++) {
726-
memdelete(old_root->get_child(i));
727-
}
728-
old_root->replace_by(new_scene);
725+
old_root->replace_by(new_scene, false, false);
729726
memdelete(old_root);
730-
edited_scene.write[p_idx].root = new_scene;
731-
if (!new_scene->get_scene_file_path().is_empty()) {
732-
edited_scene.write[p_idx].path = new_scene->get_scene_file_path();
733-
}
734727
edited_scene.write[p_idx].selection = new_selection;
735728

736729
return true;

editor/editor_node.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3818,6 +3818,14 @@ void EditorNode::_set_current_scene_nocheck(int p_idx) {
38183818
}
38193819
}
38203820

3821+
if (editor_data.check_and_update_scene(p_idx)) {
3822+
if (!editor_data.get_scene_path(p_idx).is_empty()) {
3823+
editor_folding.load_scene_folding(editor_data.get_edited_scene_root(p_idx), editor_data.get_scene_path(p_idx));
3824+
}
3825+
3826+
EditorUndoRedoManager::get_singleton()->clear_history(false, editor_data.get_scene_history_id(p_idx));
3827+
}
3828+
38213829
Dictionary state = editor_data.restore_edited_scene_state(editor_selection, &editor_history);
38223830
_edit_current(true);
38233831

@@ -3827,14 +3835,6 @@ void EditorNode::_set_current_scene_nocheck(int p_idx) {
38273835
if (tabs_to_close.is_empty()) {
38283836
callable_mp(this, &EditorNode::_set_main_scene_state).call_deferred(state, get_edited_scene()); // Do after everything else is done setting up.
38293837
}
3830-
3831-
if (editor_data.check_and_update_scene(p_idx)) {
3832-
if (!editor_data.get_scene_path(p_idx).is_empty()) {
3833-
editor_folding.load_scene_folding(editor_data.get_edited_scene_root(p_idx), editor_data.get_scene_path(p_idx));
3834-
}
3835-
3836-
EditorUndoRedoManager::get_singleton()->clear_history(false, editor_data.get_scene_history_id(p_idx));
3837-
}
38383838
}
38393839

38403840
void EditorNode::setup_color_picker(ColorPicker *p_picker) {

misc/extension_api_validation/4.2-stable.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,10 @@ Validate extension JSON: Error: Field 'classes/AcceptDialog/methods/register_tex
248248
Validate extension JSON: Error: Field 'classes/AcceptDialog/methods/remove_button/arguments/0': type changed value in new API, from "Control" to "Button".
249249

250250
Changed argument type to the more specific one actually expected by the method. Compatibility method registered.
251+
252+
253+
GH-89992
254+
--------
255+
Validate extension JSON: Error: Field 'classes/Node/methods/replace_by/arguments': size changed value in new API, from 2 to 3.
256+
257+
Added optional argument to prevent children to be reparented during replace_by. Compatibility method registered.

scene/main/node.compat.inc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**************************************************************************/
2+
/* node.compat.inc */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#ifndef DISABLE_DEPRECATED
32+
33+
void Node::_replace_by_bind_compat_89992(Node *p_node, bool p_keep_data) {
34+
replace_by(p_node, p_keep_data, true);
35+
}
36+
37+
void Node::_bind_compatibility_methods() {
38+
ClassDB::bind_compatibility_method(D_METHOD("replace_by", "node", "keep_groups"), &Node::_replace_by_bind_compat_89992, DEFVAL(false));
39+
}
40+
41+
#endif

scene/main/node.cpp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
/**************************************************************************/
3030

3131
#include "node.h"
32+
#include "node.compat.inc"
3233

3334
#include "core/config/project_settings.h"
3435
#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) {
30073008
}
30083009
}
30093010

3010-
void Node::replace_by(Node *p_node, bool p_keep_groups) {
3011+
void Node::replace_by(Node *p_node, bool p_keep_groups, bool p_keep_children) {
30113012
ERR_THREAD_GUARD
30123013
ERR_FAIL_NULL(p_node);
30133014
ERR_FAIL_COND(p_node->data.parent);
@@ -3028,13 +3029,13 @@ void Node::replace_by(Node *p_node, bool p_keep_groups) {
30283029
_replace_connections_target(p_node);
30293030

30303031
if (data.owner) {
3031-
for (int i = 0; i < get_child_count(); i++) {
3032-
find_owned_by(data.owner, get_child(i), &owned_by_owner);
3032+
if (p_keep_children) {
3033+
for (int i = 0; i < get_child_count(); i++) {
3034+
find_owned_by(data.owner, get_child(i), &owned_by_owner);
3035+
}
30333036
}
3034-
30353037
_clean_up_owner();
30363038
}
3037-
30383039
Node *parent = data.parent;
30393040
int index_in_parent = get_index(false);
30403041

@@ -3046,31 +3047,33 @@ void Node::replace_by(Node *p_node, bool p_keep_groups) {
30463047

30473048
emit_signal(SNAME("replacing_by"), p_node);
30483049

3049-
while (get_child_count()) {
3050-
Node *child = get_child(0);
3051-
remove_child(child);
3052-
if (!child->is_owned_by_parent()) {
3053-
// add the custom children to the p_node
3054-
Node *child_owner = child->get_owner() == this ? p_node : child->get_owner();
3055-
child->set_owner(nullptr);
3056-
p_node->add_child(child);
3057-
child->set_owner(child_owner);
3050+
if (p_keep_children) {
3051+
while (get_child_count()) {
3052+
Node *child = get_child(0);
3053+
remove_child(child);
3054+
if (!child->is_owned_by_parent()) {
3055+
// add the custom children to the p_node
3056+
Node *child_owner = child->get_owner() == this ? p_node : child->get_owner();
3057+
child->set_owner(nullptr);
3058+
p_node->add_child(child);
3059+
child->set_owner(child_owner);
3060+
}
30583061
}
3059-
}
30603062

3061-
p_node->set_owner(owner);
3062-
for (Node *E : owned) {
3063-
if (E->data.owner != p_node) {
3064-
E->set_owner(p_node);
3063+
for (Node *E : owned) {
3064+
if (E->data.owner != p_node) {
3065+
E->set_owner(p_node);
3066+
}
30653067
}
3066-
}
30673068

3068-
for (Node *E : owned_by_owner) {
3069-
if (E->data.owner != owner) {
3070-
E->set_owner(owner);
3069+
for (Node *E : owned_by_owner) {
3070+
if (E->data.owner != owner) {
3071+
E->set_owner(owner);
3072+
}
30713073
}
30723074
}
30733075

3076+
p_node->set_owner(owner);
30743077
p_node->set_scene_file_path(get_scene_file_path());
30753078
}
30763079

@@ -3597,7 +3600,7 @@ void Node::_bind_methods() {
35973600
ClassDB::bind_method(D_METHOD("create_tween"), &Node::create_tween);
35983601

35993602
ClassDB::bind_method(D_METHOD("duplicate", "flags"), &Node::duplicate, DEFVAL(DUPLICATE_USE_INSTANTIATION | DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS));
3600-
ClassDB::bind_method(D_METHOD("replace_by", "node", "keep_groups"), &Node::replace_by, DEFVAL(false));
3603+
ClassDB::bind_method(D_METHOD("replace_by", "node", "keep_groups", "keep_children"), &Node::replace_by, DEFVAL(false), DEFVAL(true));
36013604

36023605
ClassDB::bind_method(D_METHOD("set_scene_instance_load_placeholder", "load_placeholder"), &Node::set_scene_instance_load_placeholder);
36033606
ClassDB::bind_method(D_METHOD("get_scene_instance_load_placeholder"), &Node::get_scene_instance_load_placeholder);

scene/main/node.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ class Node : public Object {
310310
Variant _call_thread_safe_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
311311

312312
protected:
313+
#ifndef DISABLE_DEPRECATED
314+
void _replace_by_bind_compat_89992(Node *p_node, bool p_keep_data = false);
315+
static void _bind_compatibility_methods();
316+
#endif // DISABLE_DEPRECATED
317+
313318
void _block() { data.blocked++; }
314319
void _unblock() { data.blocked--; }
315320

@@ -629,7 +634,7 @@ class Node : public Object {
629634
return binds;
630635
}
631636

632-
void replace_by(Node *p_node, bool p_keep_data = false);
637+
void replace_by(Node *p_node, bool p_keep_groups = false, bool p_keep_children = true);
633638

634639
void set_process_mode(ProcessMode p_mode);
635640
ProcessMode get_process_mode() const;

0 commit comments

Comments
 (0)