Skip to content

Commit 34fc2c1

Browse files
committed
Merge pull request godotengine#106339 from Ryan-000/Run-EditorScript-from-FileSystemDock
Allow running EditorScripts from the FileSystemDock
2 parents c38b856 + e3db0d6 commit 34fc2c1

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"
@@ -5768,6 +5769,41 @@ bool EditorNode::validate_custom_directory() {
57685769
return true;
57695770
}
57705771

5772+
void EditorNode::run_editor_script(const Ref<Script> &p_script) {
5773+
Error err = p_script->reload(true); // Always hard reload the script before running.
5774+
if (err != OK || !p_script->is_valid()) {
5775+
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it contains errors, check the output log."), EditorToaster::SEVERITY_WARNING);
5776+
return;
5777+
}
5778+
5779+
// Perform additional checks on the script to evaluate if it's runnable.
5780+
5781+
bool is_runnable = true;
5782+
if (!ClassDB::is_parent_class(p_script->get_instance_base_type(), "EditorScript")) {
5783+
is_runnable = false;
5784+
5785+
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it doesn't extend EditorScript."), EditorToaster::SEVERITY_WARNING);
5786+
}
5787+
if (!p_script->is_tool()) {
5788+
is_runnable = false;
5789+
5790+
if (p_script->get_class() == "GDScript") {
5791+
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);
5792+
} else if (p_script->get_class() == "CSharpScript") {
5793+
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);
5794+
} else {
5795+
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it's not a tool script."), EditorToaster::SEVERITY_WARNING);
5796+
}
5797+
}
5798+
if (!is_runnable) {
5799+
return;
5800+
}
5801+
5802+
Ref<EditorScript> es = memnew(EditorScript);
5803+
es->set_script(p_script);
5804+
es->run();
5805+
}
5806+
57715807
void EditorNode::_immediate_dialog_confirmed() {
57725808
immediate_dialog_confirmed = true;
57735809
}

editor/editor_node.h

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

995995
bool ensure_main_scene(bool p_from_native);
996996
bool validate_custom_directory();
997+
void run_editor_script(const Ref<Script> &p_script);
997998
};
998999

9991000
class EditorPluginList : public Object {

editor/filesystem_dock.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,6 +2594,14 @@ void FileSystemDock::_file_option(int p_option, const Vector<String> &p_selected
25942594
String dir = ProjectSettings::get_singleton()->globalize_path(fpath);
25952595
ScriptEditor::get_singleton()->open_text_file_create_dialog(dir);
25962596
} break;
2597+
case FILE_MENU_RUN_SCRIPT: {
2598+
if (p_selected.size() == 1) {
2599+
const String &fpath = p_selected[0];
2600+
Ref<Script> scr = ResourceLoader::load(fpath);
2601+
ERR_FAIL_COND(scr.is_null());
2602+
EditorNode::get_singleton()->run_editor_script(scr);
2603+
}
2604+
} break;
25972605

25982606
case EXTRA_FOCUS_PATH: {
25992607
focus_on_filter();
@@ -3279,6 +3287,16 @@ void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, const Vect
32793287
p_popup->add_separator();
32803288
} else if (filenames.size() == 1) {
32813289
p_popup->add_icon_item(get_editor_theme_icon(SNAME("Load")), TTRC("Open"), FILE_MENU_OPEN);
3290+
3291+
String type = EditorFileSystem::get_singleton()->get_file_type(filenames[0]);
3292+
if (ClassDB::is_parent_class(type, "Script")) {
3293+
Ref<Script> scr = ResourceLoader::load(filenames[0]);
3294+
if (scr.is_valid()) {
3295+
if (ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorScript")) {
3296+
p_popup->add_icon_item(get_editor_theme_icon(SNAME("MainPlay")), TTRC("Run"), FILE_MENU_RUN_SCRIPT);
3297+
}
3298+
}
3299+
}
32823300
p_popup->add_separator();
32833301
}
32843302

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
@@ -1483,36 +1483,7 @@ void ScriptEditor::_menu_option(int p_option) {
14831483

14841484
current->apply_code();
14851485

1486-
Error err = scr->reload(true); // Always hard reload the script before running.
1487-
if (err != OK || !scr->is_valid()) {
1488-
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it contains errors, check the output log."), EditorToaster::SEVERITY_WARNING);
1489-
return;
1490-
}
1491-
1492-
// Perform additional checks on the script to evaluate if it's runnable.
1493-
1494-
bool is_runnable = true;
1495-
if (!ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorScript")) {
1496-
is_runnable = false;
1497-
1498-
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it doesn't extend EditorScript."), EditorToaster::SEVERITY_WARNING);
1499-
}
1500-
if (!scr->is_tool()) {
1501-
is_runnable = false;
1502-
1503-
if (scr->get_class() == "GDScript") {
1504-
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);
1505-
} else {
1506-
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it's not a tool script."), EditorToaster::SEVERITY_WARNING);
1507-
}
1508-
}
1509-
if (!is_runnable) {
1510-
return;
1511-
}
1512-
1513-
Ref<EditorScript> es = memnew(EditorScript);
1514-
es->set_script(scr);
1515-
es->run();
1486+
EditorNode::get_singleton()->run_editor_script(scr);
15161487
} break;
15171488

15181489
case FILE_MENU_CLOSE: {

0 commit comments

Comments
 (0)