From 85a1662e3bdf76d3fbe1a26ac19d92216f0f7f25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E9=9D=92=E5=B1=B1?= Date: Tue, 4 Jun 2024 22:03:11 +0800 Subject: [PATCH] Call `EditorNode::set_edited_scene()` manually instead of via the `replacing_by` signal Cannot change `scene_root`'s child node in `EditorNode::set_edited_scene()` if replaced later using `replace_by`. --- editor/editor_node.cpp | 22 ++++++++++++---------- editor/editor_node.h | 1 + editor/scene_tree_dock.cpp | 4 ++++ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index ebdad467ff6..45d276dd796 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -3706,12 +3706,15 @@ void EditorNode::_remove_scene(int index, bool p_change_tab) { } void EditorNode::set_edited_scene(Node *p_scene) { + set_edited_scene_root(p_scene, true); +} + +void EditorNode::set_edited_scene_root(Node *p_scene, bool p_auto_add) { Node *old_edited_scene_root = get_editor_data().get_edited_scene_root(); - if (old_edited_scene_root) { - if (old_edited_scene_root->get_parent() == scene_root) { - scene_root->remove_child(old_edited_scene_root); - } - old_edited_scene_root->disconnect(SNAME("replacing_by"), callable_mp(this, &EditorNode::set_edited_scene)); + ERR_FAIL_COND_MSG(p_scene && p_scene != old_edited_scene_root && p_scene->get_parent(), "Non-null nodes that are set as edited scene should not have a parent node."); + + if (p_auto_add && old_edited_scene_root && old_edited_scene_root->get_parent() == scene_root) { + scene_root->remove_child(old_edited_scene_root); } get_editor_data().set_edited_scene_root(p_scene); @@ -3723,11 +3726,8 @@ void EditorNode::set_edited_scene(Node *p_scene) { get_tree()->set_edited_scene_root(p_scene); } - if (p_scene) { - if (p_scene->get_parent() != scene_root) { - scene_root->add_child(p_scene, true); - } - p_scene->connect(SNAME("replacing_by"), callable_mp(this, &EditorNode::set_edited_scene)); + if (p_auto_add && p_scene) { + scene_root->add_child(p_scene, true); } } @@ -5967,6 +5967,8 @@ void EditorNode::reload_instances_with_path_in_edited_scenes(const String &p_ins instantiated_node->set_scene_file_path(String()); } current_edited_scene = instantiated_node; + + editor_data.set_edited_scene_root(current_edited_scene); } // Replace the original node with the instantiated version. diff --git a/editor/editor_node.h b/editor/editor_node.h index 5d7bd5b4f81..899da994501 100644 --- a/editor/editor_node.h +++ b/editor/editor_node.h @@ -783,6 +783,7 @@ public: SubViewport *get_scene_root() { return scene_root; } // Root of the scene being edited. void set_edited_scene(Node *p_scene); + void set_edited_scene_root(Node *p_scene, bool p_auto_add); Node *get_edited_scene() { return editor_data.get_edited_scene_root(); } void fix_dependencies(const String &p_for_file); diff --git a/editor/scene_tree_dock.cpp b/editor/scene_tree_dock.cpp index 2f57dc56101..c71cb9d4ac9 100644 --- a/editor/scene_tree_dock.cpp +++ b/editor/scene_tree_dock.cpp @@ -2992,6 +2992,10 @@ void SceneTreeDock::_replace_node(Node *p_node, Node *p_by_node, bool p_keep_pro to_erase.push_back(oldnode->get_child(i)); } } + + if (oldnode == edited_scene) { + EditorNode::get_singleton()->set_edited_scene_root(newnode, false); + } oldnode->replace_by(newnode, true); // Re-apply size of anchored control.