Skip to content

Commit e3db0d6

Browse files
Ryan-000KoBeWi
andcommitted
Allow running EditorScripts from the FileSystemDock
Apply suggestions from code review Co-Authored-By: Tomasz Chabora <[email protected]>
1 parent db66343 commit e3db0d6

File tree

5 files changed

+57
-30
lines changed

5 files changed

+57
-30
lines changed

editor/editor_node.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
#include "editor/editor_property_name_processor.h"
9797
#include "editor/editor_resource_picker.h"
9898
#include "editor/editor_resource_preview.h"
99+
#include "editor/editor_script.h"
99100
#include "editor/editor_settings.h"
100101
#include "editor/editor_settings_dialog.h"
101102
#include "editor/editor_translation_parser.h"
@@ -5742,6 +5743,41 @@ bool EditorNode::validate_custom_directory() {
57425743
return true;
57435744
}
57445745

5746+
void EditorNode::run_editor_script(const Ref<Script> &p_script) {
5747+
Error err = p_script->reload(true); // Always hard reload the script before running.
5748+
if (err != OK || !p_script->is_valid()) {
5749+
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it contains errors, check the output log."), EditorToaster::SEVERITY_WARNING);
5750+
return;
5751+
}
5752+
5753+
// Perform additional checks on the script to evaluate if it's runnable.
5754+
5755+
bool is_runnable = true;
5756+
if (!ClassDB::is_parent_class(p_script->get_instance_base_type(), "EditorScript")) {
5757+
is_runnable = false;
5758+
5759+
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it doesn't extend EditorScript."), EditorToaster::SEVERITY_WARNING);
5760+
}
5761+
if (!p_script->is_tool()) {
5762+
is_runnable = false;
5763+
5764+
if (p_script->get_class() == "GDScript") {
5765+
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it's not a tool script (add the @tool annotation at the top)."), EditorToaster::SEVERITY_WARNING);
5766+
} else if (p_script->get_class() == "CSharpScript") {
5767+
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it's not a tool script (add the [Tool] attribute above the class definition)."), EditorToaster::SEVERITY_WARNING);
5768+
} else {
5769+
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it's not a tool script."), EditorToaster::SEVERITY_WARNING);
5770+
}
5771+
}
5772+
if (!is_runnable) {
5773+
return;
5774+
}
5775+
5776+
Ref<EditorScript> es = memnew(EditorScript);
5777+
es->set_script(p_script);
5778+
es->run();
5779+
}
5780+
57455781
void EditorNode::_immediate_dialog_confirmed() {
57465782
immediate_dialog_confirmed = true;
57475783
}

editor/editor_node.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,7 @@ class EditorNode : public Node {
990990

991991
bool ensure_main_scene(bool p_from_native);
992992
bool validate_custom_directory();
993+
void run_editor_script(const Ref<Script> &p_script);
993994
};
994995

995996
class EditorPluginList : public Object {

editor/filesystem_dock.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,6 +2588,14 @@ void FileSystemDock::_file_option(int p_option, const Vector<String> &p_selected
25882588
String dir = ProjectSettings::get_singleton()->globalize_path(fpath);
25892589
ScriptEditor::get_singleton()->open_text_file_create_dialog(dir);
25902590
} break;
2591+
case FILE_MENU_RUN_SCRIPT: {
2592+
if (p_selected.size() == 1) {
2593+
const String &fpath = p_selected[0];
2594+
Ref<Script> scr = ResourceLoader::load(fpath);
2595+
ERR_FAIL_COND(scr.is_null());
2596+
EditorNode::get_singleton()->run_editor_script(scr);
2597+
}
2598+
} break;
25912599

25922600
case EXTRA_FOCUS_PATH: {
25932601
focus_on_filter();
@@ -3273,6 +3281,16 @@ void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, const Vect
32733281
p_popup->add_separator();
32743282
} else if (filenames.size() == 1) {
32753283
p_popup->add_icon_item(get_editor_theme_icon(SNAME("Load")), TTRC("Open"), FILE_MENU_OPEN);
3284+
3285+
String type = EditorFileSystem::get_singleton()->get_file_type(filenames[0]);
3286+
if (ClassDB::is_parent_class(type, "Script")) {
3287+
Ref<Script> scr = ResourceLoader::load(filenames[0]);
3288+
if (scr.is_valid()) {
3289+
if (ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorScript")) {
3290+
p_popup->add_icon_item(get_editor_theme_icon(SNAME("MainPlay")), TTRC("Run"), FILE_MENU_RUN_SCRIPT);
3291+
}
3292+
}
3293+
}
32763294
p_popup->add_separator();
32773295
}
32783296

editor/filesystem_dock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class FileSystemDock : public VBoxContainer {
129129
FILE_MENU_NEW_FOLDER,
130130
FILE_MENU_NEW_SCRIPT,
131131
FILE_MENU_NEW_SCENE,
132+
FILE_MENU_RUN_SCRIPT,
132133
FILE_MENU_MAX,
133134
// Extra shortcuts that don't exist in the menu.
134135
EXTRA_FOCUS_PATH,

editor/plugins/script_editor_plugin.cpp

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,36 +1491,7 @@ void ScriptEditor::_menu_option(int p_option) {
14911491

14921492
current->apply_code();
14931493

1494-
Error err = scr->reload(true); // Always hard reload the script before running.
1495-
if (err != OK || !scr->is_valid()) {
1496-
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it contains errors, check the output log."), EditorToaster::SEVERITY_WARNING);
1497-
return;
1498-
}
1499-
1500-
// Perform additional checks on the script to evaluate if it's runnable.
1501-
1502-
bool is_runnable = true;
1503-
if (!ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorScript")) {
1504-
is_runnable = false;
1505-
1506-
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it doesn't extend EditorScript."), EditorToaster::SEVERITY_WARNING);
1507-
}
1508-
if (!scr->is_tool()) {
1509-
is_runnable = false;
1510-
1511-
if (scr->get_class() == "GDScript") {
1512-
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it's not a tool script (add the @tool annotation at the top)."), EditorToaster::SEVERITY_WARNING);
1513-
} else {
1514-
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it's not a tool script."), EditorToaster::SEVERITY_WARNING);
1515-
}
1516-
}
1517-
if (!is_runnable) {
1518-
return;
1519-
}
1520-
1521-
Ref<EditorScript> es = memnew(EditorScript);
1522-
es->set_script(scr);
1523-
es->run();
1494+
EditorNode::get_singleton()->run_editor_script(scr);
15241495
} break;
15251496

15261497
case FILE_MENU_CLOSE: {

0 commit comments

Comments
 (0)