Skip to content

Commit 9638220

Browse files
committed
Fix reloading scripts already in use
1 parent 694d3c2 commit 9638220

File tree

6 files changed

+43
-16
lines changed

6 files changed

+43
-16
lines changed

core/object/script_language.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,17 @@ void Script::reload_from_file() {
191191
set_source_code(rel->get_source_code());
192192
set_last_modified_time(rel->get_last_modified_time());
193193

194-
reload();
194+
// Only reload the script when there are no compilation errors to prevent printing the error messages twice.
195+
if (rel->is_valid()) {
196+
if (Engine::get_singleton()->is_editor_hint() && is_tool()) {
197+
get_language()->reload_tool_script(this, true);
198+
} else {
199+
// It's important to set p_keep_state to true in order to manage reloading scripts
200+
// that are currently instantiated.
201+
reload(true);
202+
}
203+
}
204+
195205
#else
196206
Resource::reload_from_file();
197207
#endif

core/object/script_language.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ class Script : public Resource {
112112
OBJ_SAVE_TYPE(Script);
113113

114114
protected:
115-
virtual bool editor_can_reload_from_file() override { return false; } // this is handled by editor better
115+
// Scripts are reloaded via the Script Editor when edited in Godot,
116+
// the LSP server when edited in a connected external editor, or
117+
// through EditorFileSystem::_update_script_documentation when updated directly on disk.
118+
virtual bool editor_can_reload_from_file() override { return false; }
116119
void _notification(int p_what);
117120
static void _bind_methods();
118121

editor/editor_file_system.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,18 +1973,16 @@ void EditorFileSystem::_update_script_documentation() {
19731973
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
19741974
ScriptLanguage *lang = ScriptServer::get_language(i);
19751975
if (lang->supports_documentation() && efd->files[index]->type == lang->get_type()) {
1976-
// Reloading the script from disk if resource already in memory. Otherwise, the
1977-
// ResourceLoader::load will return the last loaded version of the script (without the modifications).
1978-
// The only have the script already loaded here is to edit the script outside the
1979-
// editor without being connected to the LSP server.
1980-
Ref<Resource> res = ResourceCache::get_ref(path);
1981-
if (res.is_valid()) {
1982-
res->reload_from_file();
1983-
}
1976+
bool should_reload_script = _should_reload_script(path);
19841977
Ref<Script> scr = ResourceLoader::load(path);
19851978
if (scr.is_null()) {
19861979
continue;
19871980
}
1981+
if (should_reload_script) {
1982+
// Reloading the script from disk. Otherwise, the ResourceLoader::load will
1983+
// return the last loaded version of the script (without the modifications).
1984+
scr->reload_from_file();
1985+
}
19881986
Vector<DocData::ClassDoc> docs = scr->get_documentation();
19891987
for (int j = 0; j < docs.size(); j++) {
19901988
EditorHelp::get_doc_data()->add_doc(docs[j]);
@@ -2006,6 +2004,25 @@ void EditorFileSystem::_update_script_documentation() {
20062004
update_script_paths_documentation.clear();
20072005
}
20082006

2007+
bool EditorFileSystem::_should_reload_script(const String &p_path) {
2008+
if (first_scan) {
2009+
return false;
2010+
}
2011+
2012+
Ref<Script> scr = ResourceCache::get_ref(p_path);
2013+
if (scr.is_null()) {
2014+
// Not a script or not already loaded.
2015+
return false;
2016+
}
2017+
2018+
// Scripts are reloaded via the script editor if they are currently opened.
2019+
if (ScriptEditor::get_singleton()->get_open_scripts().has(scr)) {
2020+
return false;
2021+
}
2022+
2023+
return true;
2024+
}
2025+
20092026
void EditorFileSystem::_process_update_pending() {
20102027
_update_script_classes();
20112028
// Parse documentation second, as it requires the class names to be loaded

editor/editor_file_system.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ class EditorFileSystem : public Node {
295295
void _update_script_documentation();
296296
void _process_update_pending();
297297
void _process_removed_files(const HashSet<String> &p_processed_files);
298+
bool _should_reload_script(const String &p_path);
298299

299300
Mutex update_scene_mutex;
300301
HashSet<String> update_scene_paths;

editor/editor_node.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -926,11 +926,7 @@ void EditorNode::_resources_changed(const Vector<String> &p_resources) {
926926
}
927927

928928
if (!res->editor_can_reload_from_file()) {
929-
Ref<Script> scr = res;
930-
// Scripts are reloaded via the script editor.
931-
if (scr.is_null() || ScriptEditor::get_singleton()->get_open_scripts().has(scr)) {
932-
continue;
933-
}
929+
continue;
934930
}
935931
if (!res->get_path().is_resource_file() && !res->get_path().is_absolute_path()) {
936932
continue;

editor/plugins/script_editor_plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ void ScriptEditor::_menu_option(int p_option) {
14831483

14841484
current->apply_code();
14851485

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

0 commit comments

Comments
 (0)