Skip to content

Commit d9078d8

Browse files
committed
Merge pull request #109049 from ryevdokimov/deprecate-script-add-root-node
Relocate `add_root_node` method to `EditorInterface` from `EditorScript` and deprecate old method
2 parents a0afcdd + 11a4961 commit d9078d8

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

doc/classes/EditorInterface.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
<tutorials>
2020
</tutorials>
2121
<methods>
22+
<method name="add_root_node">
23+
<return type="void" />
24+
<param index="0" name="node" type="Node" />
25+
<description>
26+
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.
27+
</description>
28+
</method>
2229
<method name="close_scene">
2330
<return type="int" enum="Error" />
2431
<description>

doc/classes/EditorScript.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
This method is executed by the Editor when [b]File &gt; Run[/b] is used.
4141
</description>
4242
</method>
43-
<method name="add_root_node">
43+
<method name="add_root_node" deprecated="Use [method EditorInterface.add_root_node] instead.">
4444
<return type="void" />
4545
<param index="0" name="node" type="Node" />
4646
<description>

editor/editor_interface.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "editor_interface.compat.inc"
3333

3434
#include "core/config/project_settings.h"
35+
#include "core/io/resource_loader.h"
3536
#include "editor/docks/filesystem_dock.h"
3637
#include "editor/docks/inspector_dock.h"
3738
#include "editor/editor_main_screen.h"
@@ -58,6 +59,7 @@
5859
#include "scene/gui/box_container.h"
5960
#include "scene/gui/control.h"
6061
#include "scene/main/window.h"
62+
#include "scene/resources/packed_scene.h"
6163
#include "scene/resources/theme.h"
6264

6365
EditorInterface *EditorInterface::singleton = nullptr;
@@ -363,6 +365,28 @@ void EditorInterface::make_scene_preview(const String &p_path, Node *p_scene, in
363365
EditorFileSystem::get_singleton()->emit_signal(SNAME("filesystem_changed"));
364366
}
365367

368+
void EditorInterface::add_root_node(Node *p_node) {
369+
if (EditorNode::get_singleton()->get_edited_scene()) {
370+
ERR_PRINT("EditorInterface::add_root_node: The current scene already has a root node.");
371+
return;
372+
}
373+
374+
const String &scene_path = p_node->get_scene_file_path();
375+
if (!scene_path.is_empty()) {
376+
Ref<PackedScene> scene = ResourceLoader::load(scene_path);
377+
if (scene.is_valid()) {
378+
memfree(scene->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE)); // Ensure node cache.
379+
380+
p_node->set_scene_inherited_state(scene->get_state());
381+
p_node->set_scene_file_path(String());
382+
}
383+
}
384+
385+
EditorNode::get_singleton()->set_edited_scene(p_node);
386+
EditorUndoRedoManager::get_singleton()->set_history_as_unsaved(EditorNode::get_editor_data().get_current_edited_scene_history_id());
387+
EditorSceneTabs::get_singleton()->update_scene_tabs();
388+
}
389+
366390
void EditorInterface::set_plugin_enabled(const String &p_plugin, bool p_enabled) {
367391
EditorNode::get_singleton()->set_addon_plugin_enabled(p_plugin, p_enabled, true);
368392
}
@@ -870,6 +894,8 @@ void EditorInterface::_bind_methods() {
870894
ClassDB::bind_method(D_METHOD("get_open_scene_roots"), &EditorInterface::get_open_scene_roots);
871895
ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &EditorInterface::get_edited_scene_root);
872896

897+
ClassDB::bind_method(D_METHOD("add_root_node", "node"), &EditorInterface::add_root_node);
898+
873899
ClassDB::bind_method(D_METHOD("save_scene"), &EditorInterface::save_scene);
874900
ClassDB::bind_method(D_METHOD("save_scene_as", "path", "with_preview"), &EditorInterface::save_scene_as, DEFVAL(true));
875901
ClassDB::bind_method(D_METHOD("save_all_scenes"), &EditorInterface::save_all_scenes);

editor/editor_interface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ class EditorInterface : public Object {
178178
TypedArray<Node> get_open_scene_roots() const;
179179
Node *get_edited_scene_root() const;
180180

181+
void add_root_node(Node *p_node);
182+
181183
Error save_scene();
182184
void save_scene_as(const String &p_scene, bool p_with_preview = true);
183185
void mark_scene_as_unsaved();

editor/script/editor_script.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,8 @@
3838
#include "scene/resources/packed_scene.h"
3939

4040
void EditorScript::add_root_node(Node *p_node) {
41-
if (!EditorNode::get_singleton()) {
42-
EditorNode::add_io_error("EditorScript::add_root_node: " + TTR("Write your logic in the _run() method."));
43-
return;
44-
}
45-
46-
if (EditorNode::get_singleton()->get_edited_scene()) {
47-
EditorNode::add_io_error("EditorScript::add_root_node: " + TTR("The current scene already has a root node."));
48-
return;
49-
}
50-
51-
const String &scene_path = p_node->get_scene_file_path();
52-
if (!scene_path.is_empty()) {
53-
Ref<PackedScene> scene = ResourceLoader::load(scene_path);
54-
if (scene.is_valid()) {
55-
memfree(scene->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE)); // Ensure node cache.
56-
57-
p_node->set_scene_inherited_state(scene->get_state());
58-
p_node->set_scene_file_path(String());
59-
}
60-
}
61-
62-
EditorNode::get_singleton()->set_edited_scene(p_node);
63-
EditorUndoRedoManager::get_singleton()->set_history_as_unsaved(EditorNode::get_editor_data().get_current_edited_scene_history_id());
64-
EditorSceneTabs::get_singleton()->update_scene_tabs();
41+
WARN_DEPRECATED_MSG("EditorScript::add_root_node is deprecated. Use EditorInterface::add_root_node instead.");
42+
EditorInterface::get_singleton()->add_root_node(p_node);
6543
}
6644

6745
Node *EditorScript::get_scene() const {

0 commit comments

Comments
 (0)