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 adae6599c17..2b51071b150 100644 --- a/editor/editor_file_system.cpp +++ b/editor/editor_file_system.cpp @@ -1974,18 +1974,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