Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
#include "editor/editor_property_name_processor.h"
#include "editor/editor_resource_picker.h"
#include "editor/editor_resource_preview.h"
#include "editor/editor_script.h"
#include "editor/editor_settings.h"
#include "editor/editor_settings_dialog.h"
#include "editor/editor_translation_parser.h"
Expand Down Expand Up @@ -5742,6 +5743,41 @@ bool EditorNode::validate_custom_directory() {
return true;
}

void EditorNode::run_editor_script(const Ref<Script> &p_script) {
Error err = p_script->reload(true); // Always hard reload the script before running.
if (err != OK || !p_script->is_valid()) {
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it contains errors, check the output log."), EditorToaster::SEVERITY_WARNING);
return;
}

// Perform additional checks on the script to evaluate if it's runnable.

bool is_runnable = true;
if (!ClassDB::is_parent_class(p_script->get_instance_base_type(), "EditorScript")) {
is_runnable = false;

EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it doesn't extend EditorScript."), EditorToaster::SEVERITY_WARNING);
}
if (!p_script->is_tool()) {
is_runnable = false;

if (p_script->get_class() == "GDScript") {
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);
} else if (p_script->get_class() == "CSharpScript") {
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);
} else {
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it's not a tool script."), EditorToaster::SEVERITY_WARNING);
}
}
if (!is_runnable) {
return;
}

Ref<EditorScript> es = memnew(EditorScript);
es->set_script(p_script);
es->run();
}

void EditorNode::_immediate_dialog_confirmed() {
immediate_dialog_confirmed = true;
}
Expand Down
1 change: 1 addition & 0 deletions editor/editor_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@ class EditorNode : public Node {

bool ensure_main_scene(bool p_from_native);
bool validate_custom_directory();
void run_editor_script(const Ref<Script> &p_script);
};

class EditorPluginList : public Object {
Expand Down
18 changes: 18 additions & 0 deletions editor/filesystem_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2588,6 +2588,14 @@ void FileSystemDock::_file_option(int p_option, const Vector<String> &p_selected
String dir = ProjectSettings::get_singleton()->globalize_path(fpath);
ScriptEditor::get_singleton()->open_text_file_create_dialog(dir);
} break;
case FILE_MENU_RUN_SCRIPT: {
if (p_selected.size() == 1) {
const String &fpath = p_selected[0];
Ref<Script> scr = ResourceLoader::load(fpath);
ERR_FAIL_COND(scr.is_null());
EditorNode::get_singleton()->run_editor_script(scr);
}
} break;

case EXTRA_FOCUS_PATH: {
focus_on_filter();
Expand Down Expand Up @@ -3273,6 +3281,16 @@ void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, const Vect
p_popup->add_separator();
} else if (filenames.size() == 1) {
p_popup->add_icon_item(get_editor_theme_icon(SNAME("Load")), TTRC("Open"), FILE_MENU_OPEN);

String type = EditorFileSystem::get_singleton()->get_file_type(filenames[0]);
if (ClassDB::is_parent_class(type, "Script")) {
Ref<Script> scr = ResourceLoader::load(filenames[0]);
if (scr.is_valid()) {
if (ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorScript")) {
p_popup->add_icon_item(get_editor_theme_icon(SNAME("MainPlay")), TTRC("Run"), FILE_MENU_RUN_SCRIPT);
}
}
}
p_popup->add_separator();
}

Expand Down
1 change: 1 addition & 0 deletions editor/filesystem_dock.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class FileSystemDock : public VBoxContainer {
FILE_MENU_NEW_FOLDER,
FILE_MENU_NEW_SCRIPT,
FILE_MENU_NEW_SCENE,
FILE_MENU_RUN_SCRIPT,
FILE_MENU_MAX,
// Extra shortcuts that don't exist in the menu.
EXTRA_FOCUS_PATH,
Expand Down
31 changes: 1 addition & 30 deletions editor/plugins/script_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1491,36 +1491,7 @@ void ScriptEditor::_menu_option(int p_option) {

current->apply_code();

Error err = scr->reload(true); // Always hard reload the script before running.
if (err != OK || !scr->is_valid()) {
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it contains errors, check the output log."), EditorToaster::SEVERITY_WARNING);
return;
}

// Perform additional checks on the script to evaluate if it's runnable.

bool is_runnable = true;
if (!ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorScript")) {
is_runnable = false;

EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it doesn't extend EditorScript."), EditorToaster::SEVERITY_WARNING);
}
if (!scr->is_tool()) {
is_runnable = false;

if (scr->get_class() == "GDScript") {
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);
} else {
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it's not a tool script."), EditorToaster::SEVERITY_WARNING);
}
}
if (!is_runnable) {
return;
}

Ref<EditorScript> es = memnew(EditorScript);
es->set_script(scr);
es->run();
EditorNode::get_singleton()->run_editor_script(scr);
} break;

case FILE_MENU_CLOSE: {
Expand Down