From 96382204736cbc131fbc2640ba6ba238c53017c0 Mon Sep 17 00:00:00 2001 From: Hilderin <81109165+Hilderin@users.noreply.github.com> Date: Wed, 18 Sep 2024 19:37:12 -0400 Subject: [PATCH] Fix reloading scripts already in use --- core/object/script_language.cpp | 12 ++++++++- core/object/script_language.h | 5 +++- editor/editor_file_system.cpp | 33 +++++++++++++++++++------ editor/editor_file_system.h | 1 + editor/editor_node.cpp | 6 +---- editor/plugins/script_editor_plugin.cpp | 2 +- 6 files changed, 43 insertions(+), 16 deletions(-) diff --git a/core/object/script_language.cpp b/core/object/script_language.cpp index d2fc7392c86..c5856a8a81d 100644 --- a/core/object/script_language.cpp +++ b/core/object/script_language.cpp @@ -191,7 +191,17 @@ void Script::reload_from_file() { set_source_code(rel->get_source_code()); set_last_modified_time(rel->get_last_modified_time()); - reload(); + // Only reload the script when there are no compilation errors to prevent printing the error messages twice. + if (rel->is_valid()) { + if (Engine::get_singleton()->is_editor_hint() && is_tool()) { + get_language()->reload_tool_script(this, true); + } else { + // It's important to set p_keep_state to true in order to manage reloading scripts + // that are currently instantiated. + reload(true); + } + } + #else Resource::reload_from_file(); #endif diff --git a/core/object/script_language.h b/core/object/script_language.h index d0023d70e8c..3ddfbb3e7d3 100644 --- a/core/object/script_language.h +++ b/core/object/script_language.h @@ -112,7 +112,10 @@ class Script : public Resource { OBJ_SAVE_TYPE(Script); protected: - virtual bool editor_can_reload_from_file() override { return false; } // this is handled by editor better + // Scripts are reloaded via the Script Editor when edited in Godot, + // the LSP server when edited in a connected external editor, or + // through EditorFileSystem::_update_script_documentation when updated directly on disk. + virtual bool editor_can_reload_from_file() override { return false; } void _notification(int p_what); static void _bind_methods(); diff --git a/editor/editor_file_system.cpp b/editor/editor_file_system.cpp index cad615e6c5b..bee7fdf7b24 100644 --- a/editor/editor_file_system.cpp +++ b/editor/editor_file_system.cpp @@ -1973,18 +1973,16 @@ void EditorFileSystem::_update_script_documentation() { for (int i = 0; i < ScriptServer::get_language_count(); i++) { ScriptLanguage *lang = ScriptServer::get_language(i); if (lang->supports_documentation() && efd->files[index]->type == lang->get_type()) { - // Reloading the script from disk if resource already in memory. Otherwise, the - // ResourceLoader::load will return the last loaded version of the script (without the modifications). - // The only have the script already loaded here is to edit the script outside the - // editor without being connected to the LSP server. - Ref res = ResourceCache::get_ref(path); - if (res.is_valid()) { - res->reload_from_file(); - } + bool should_reload_script = _should_reload_script(path); Ref