From 11a4961081dfd7919087b200a25fccf7639df951 Mon Sep 17 00:00:00 2001 From: robert yevdokimov Date: Mon, 28 Jul 2025 17:04:42 +0400 Subject: [PATCH] Relocate `add_root_node` method to `EditorInterface` from `EditorScript` and deprecate old method --- doc/classes/EditorInterface.xml | 7 +++++++ doc/classes/EditorScript.xml | 2 +- editor/editor_interface.cpp | 26 ++++++++++++++++++++++++++ editor/editor_interface.h | 2 ++ editor/script/editor_script.cpp | 26 ++------------------------ 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/doc/classes/EditorInterface.xml b/doc/classes/EditorInterface.xml index d0a7177fb68..677c33aa4fd 100644 --- a/doc/classes/EditorInterface.xml +++ b/doc/classes/EditorInterface.xml @@ -19,6 +19,13 @@ + + + + + Makes [param node] root of the currently opened scene. Only works if the scene is empty. If the [param node] is a scene instance, an inheriting scene will be created. + + diff --git a/doc/classes/EditorScript.xml b/doc/classes/EditorScript.xml index a18ed01c3d0..fe164f4471b 100644 --- a/doc/classes/EditorScript.xml +++ b/doc/classes/EditorScript.xml @@ -41,7 +41,7 @@ This method is executed by the Editor when [b]File > Run[/b] is used. - + diff --git a/editor/editor_interface.cpp b/editor/editor_interface.cpp index 51645cee54a..1c2c74fb8c3 100644 --- a/editor/editor_interface.cpp +++ b/editor/editor_interface.cpp @@ -32,6 +32,7 @@ #include "editor_interface.compat.inc" #include "core/config/project_settings.h" +#include "core/io/resource_loader.h" #include "editor/docks/filesystem_dock.h" #include "editor/docks/inspector_dock.h" #include "editor/editor_main_screen.h" @@ -58,6 +59,7 @@ #include "scene/gui/box_container.h" #include "scene/gui/control.h" #include "scene/main/window.h" +#include "scene/resources/packed_scene.h" #include "scene/resources/theme.h" EditorInterface *EditorInterface::singleton = nullptr; @@ -360,6 +362,28 @@ void EditorInterface::make_scene_preview(const String &p_path, Node *p_scene, in EditorFileSystem::get_singleton()->emit_signal(SNAME("filesystem_changed")); } +void EditorInterface::add_root_node(Node *p_node) { + if (EditorNode::get_singleton()->get_edited_scene()) { + ERR_PRINT("EditorInterface::add_root_node: The current scene already has a root node."); + return; + } + + const String &scene_path = p_node->get_scene_file_path(); + if (!scene_path.is_empty()) { + Ref scene = ResourceLoader::load(scene_path); + if (scene.is_valid()) { + memfree(scene->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE)); // Ensure node cache. + + p_node->set_scene_inherited_state(scene->get_state()); + p_node->set_scene_file_path(String()); + } + } + + EditorNode::get_singleton()->set_edited_scene(p_node); + EditorUndoRedoManager::get_singleton()->set_history_as_unsaved(EditorNode::get_editor_data().get_current_edited_scene_history_id()); + EditorSceneTabs::get_singleton()->update_scene_tabs(); +} + void EditorInterface::set_plugin_enabled(const String &p_plugin, bool p_enabled) { EditorNode::get_singleton()->set_addon_plugin_enabled(p_plugin, p_enabled, true); } @@ -846,6 +870,8 @@ void EditorInterface::_bind_methods() { ClassDB::bind_method(D_METHOD("get_open_scene_roots"), &EditorInterface::get_open_scene_roots); ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &EditorInterface::get_edited_scene_root); + ClassDB::bind_method(D_METHOD("add_root_node", "node"), &EditorInterface::add_root_node); + ClassDB::bind_method(D_METHOD("save_scene"), &EditorInterface::save_scene); ClassDB::bind_method(D_METHOD("save_scene_as", "path", "with_preview"), &EditorInterface::save_scene_as, DEFVAL(true)); ClassDB::bind_method(D_METHOD("save_all_scenes"), &EditorInterface::save_all_scenes); diff --git a/editor/editor_interface.h b/editor/editor_interface.h index 3b95eb0e7d7..f894d4b59d1 100644 --- a/editor/editor_interface.h +++ b/editor/editor_interface.h @@ -173,6 +173,8 @@ public: TypedArray get_open_scene_roots() const; Node *get_edited_scene_root() const; + void add_root_node(Node *p_node); + Error save_scene(); void save_scene_as(const String &p_scene, bool p_with_preview = true); void mark_scene_as_unsaved(); diff --git a/editor/script/editor_script.cpp b/editor/script/editor_script.cpp index 0c96044f7a9..108bf563240 100644 --- a/editor/script/editor_script.cpp +++ b/editor/script/editor_script.cpp @@ -38,30 +38,8 @@ #include "scene/resources/packed_scene.h" void EditorScript::add_root_node(Node *p_node) { - if (!EditorNode::get_singleton()) { - EditorNode::add_io_error("EditorScript::add_root_node: " + TTR("Write your logic in the _run() method.")); - return; - } - - if (EditorNode::get_singleton()->get_edited_scene()) { - EditorNode::add_io_error("EditorScript::add_root_node: " + TTR("The current scene already has a root node.")); - return; - } - - const String &scene_path = p_node->get_scene_file_path(); - if (!scene_path.is_empty()) { - Ref scene = ResourceLoader::load(scene_path); - if (scene.is_valid()) { - memfree(scene->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE)); // Ensure node cache. - - p_node->set_scene_inherited_state(scene->get_state()); - p_node->set_scene_file_path(String()); - } - } - - EditorNode::get_singleton()->set_edited_scene(p_node); - EditorUndoRedoManager::get_singleton()->set_history_as_unsaved(EditorNode::get_editor_data().get_current_edited_scene_history_id()); - EditorSceneTabs::get_singleton()->update_scene_tabs(); + WARN_DEPRECATED_MSG("EditorScript::add_root_node is deprecated. Use EditorInterface::add_root_node instead."); + EditorInterface::get_singleton()->add_root_node(p_node); } Node *EditorScript::get_scene() const {