Skip to content

Commit 11a4961

Browse files
committed
Relocate add_root_node method to EditorInterface from EditorScript and deprecate old method
1 parent 8b2739e commit 11a4961

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
@@ -41,7 +41,7 @@
4141
This method is executed by the Editor when [b]File &gt; Run[/b] is used.
4242
</description>
4343
</method>
44-
<method name="add_root_node">
44+
<method name="add_root_node" deprecated="Use [method EditorInterface.add_root_node] instead.">
4545
<return type="void" />
4646
<param index="0" name="node" type="Node" />
4747
<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;
@@ -360,6 +362,28 @@ void EditorInterface::make_scene_preview(const String &p_path, Node *p_scene, in
360362
EditorFileSystem::get_singleton()->emit_signal(SNAME("filesystem_changed"));
361363
}
362364

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

873+
ClassDB::bind_method(D_METHOD("add_root_node", "node"), &EditorInterface::add_root_node);
874+
849875
ClassDB::bind_method(D_METHOD("save_scene"), &EditorInterface::save_scene);
850876
ClassDB::bind_method(D_METHOD("save_scene_as", "path", "with_preview"), &EditorInterface::save_scene_as, DEFVAL(true));
851877
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
@@ -173,6 +173,8 @@ class EditorInterface : public Object {
173173
TypedArray<Node> get_open_scene_roots() const;
174174
Node *get_edited_scene_root() const;
175175

176+
void add_root_node(Node *p_node);
177+
176178
Error save_scene();
177179
void save_scene_as(const String &p_scene, bool p_with_preview = true);
178180
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)