Skip to content

Commit f47f4a0

Browse files
authored
Merge pull request godotengine#89992 from ajreckof/fix-my-mistake-with-replace-in-update-scene
Fix node duplication in update after external changes.
2 parents 29b3d9e + ae47286 commit f47f4a0

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"
@@ -3005,7 +3006,7 @@ static void find_owned_by(Node *p_by, Node *p_node, List<Node *> *p_owned) {
30053006
}
30063007
}
30073008

3008-
void Node::replace_by(Node *p_node, bool p_keep_groups) {
3009+
void Node::replace_by(Node *p_node, bool p_keep_groups, bool p_keep_children) {
30093010
ERR_THREAD_GUARD
30103011
ERR_FAIL_NULL(p_node);
30113012
ERR_FAIL_COND(p_node->data.parent);
@@ -3026,13 +3027,13 @@ void Node::replace_by(Node *p_node, bool p_keep_groups) {
30263027
_replace_connections_target(p_node);
30273028

30283029
if (data.owner) {
3029-
for (int i = 0; i < get_child_count(); i++) {
3030-
find_owned_by(data.owner, get_child(i), &owned_by_owner);
3030+
if (p_keep_children) {
3031+
for (int i = 0; i < get_child_count(); i++) {
3032+
find_owned_by(data.owner, get_child(i), &owned_by_owner);
3033+
}
30313034
}
3032-
30333035
_clean_up_owner();
30343036
}
3035-
30363037
Node *parent = data.parent;
30373038
int index_in_parent = get_index(false);
30383039

@@ -3044,31 +3045,33 @@ void Node::replace_by(Node *p_node, bool p_keep_groups) {
30443045

30453046
emit_signal(SNAME("replacing_by"), p_node);
30463047

3047-
while (get_child_count()) {
3048-
Node *child = get_child(0);
3049-
remove_child(child);
3050-
if (!child->is_owned_by_parent()) {
3051-
// add the custom children to the p_node
3052-
Node *child_owner = child->get_owner() == this ? p_node : child->get_owner();
3053-
child->set_owner(nullptr);
3054-
p_node->add_child(child);
3055-
child->set_owner(child_owner);
3048+
if (p_keep_children) {
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);
3058+
}
30563059
}
3057-
}
30583060

3059-
p_node->set_owner(owner);
3060-
for (Node *E : owned) {
3061-
if (E->data.owner != p_node) {
3062-
E->set_owner(p_node);
3061+
for (Node *E : owned) {
3062+
if (E->data.owner != p_node) {
3063+
E->set_owner(p_node);
3064+
}
30633065
}
3064-
}
30653066

3066-
for (Node *E : owned_by_owner) {
3067-
if (E->data.owner != owner) {
3068-
E->set_owner(owner);
3067+
for (Node *E : owned_by_owner) {
3068+
if (E->data.owner != owner) {
3069+
E->set_owner(owner);
3070+
}
30693071
}
30703072
}
30713073

3074+
p_node->set_owner(owner);
30723075
p_node->set_scene_file_path(get_scene_file_path());
30733076
}
30743077

@@ -3595,7 +3598,7 @@ void Node::_bind_methods() {
35953598
ClassDB::bind_method(D_METHOD("create_tween"), &Node::create_tween);
35963599

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

36003603
ClassDB::bind_method(D_METHOD("set_scene_instance_load_placeholder", "load_placeholder"), &Node::set_scene_instance_load_placeholder);
36013604
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)