Skip to content

Commit 621cadc

Browse files
authored
Merge pull request #97168 from Hilderin/fix-reloading-scripts-already-in-use
Fix reloading scripts already in use
2 parents 2be730a + 9638220 commit 621cadc

6 files changed

+43
-16
lines changed

core/object/script_language.cpp

+11-1
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

+4-1
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

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

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

editor/editor_file_system.h

+1
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

+1-5
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

+1-1
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)