diff --git a/core/object/script_language.cpp b/core/object/script_language.cpp index d2fc7392c86d..c5856a8a81d6 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 d0023d70e8ca..3ddfbb3e7d35 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 cad615e6c5bc..3341070ee4b0 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