Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reloading scripts already in use #97168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Hilderin
Copy link
Contributor

Passing the parameter p_keep_state to reload in Script::reload_from_file fixed the issue.

But, I saw another problem when the scripts are opened in the editor, they were reloaded twice, once in the Script Editor and another time in EditorFileSystem. I added a check in EditorFileSystem::_update_script_documentation to reload script only if they are not opened in the Script Editor.

@ryevdokimov
Copy link
Contributor

Could you elaborate on the other problem? If I modify a single script externally the reload is still being fired multiple times based on a print_line() test, but maybe I'm misunderstanding something.

Also, not sure if you want to fix this here, but this comment from your related previous PR is confusing.

I'm assuming you meant: "The only reason the script is already loaded here is to allow editing it outside the editor, without being connected to the LSP server."

// The only have the script already loaded here is to edit the script outside the
// editor without being connected to the LSP server.

@AThousandShips AThousandShips added this to the 4.4 milestone Sep 19, 2024
@Hilderin Hilderin force-pushed the fix-reloading-scripts-already-in-use branch from fafc608 to a42d379 Compare September 19, 2024 10:35
@Hilderin
Copy link
Contributor Author

Could you elaborate on the other problem? If I modify a single script externally the reload is still being fired multiple times based on a print_line() test, but maybe I'm misunderstanding something.

The problem I fixed was related to using the internal editor. However, you are right — using an external editor connected to the LSP server (e.g., VSCode) was causing a double reload. If you edit the file in Notepad, for example, or use git pull to update a GDScript while VSCode is closed, there’s no LSP server to inform Godot that the script was updated. What can be tricky is that even if you update the file in Notepad while VSCode is open and connected via LSP, it’s the LSP server that will trigger the script reload. However, after some testing, it’s not always the case, and I couldn’t find a consistent pattern.

After further investigation, I found that scripts were already being reloaded twice when edited in an external editor connected via LSP — once from GDScriptTextDocument::didSave and once in EditorNode::_resources_changed. I made additional modifications to prevent reloading the script more than once when saved via an external editor, whether LSP is used or not. Using the get_last_modified_time of the resource and comparing it with the file’s last modification time on disk in EditorFileSystem::_should_reload_script and EditorNode::_resources_changed solved the issue.

Additionally, I discovered that both the ScriptEditor and LSP server use get_language()->reload_tool_script to reload tool scripts. I couldn't see any difference between that and a simple reload(true), but just in case, I modified Script::reload_from_file to do exactly the same thing.

Note: The GDScript::reload method is still called twice when invoking Script::reload_from_file — once in GDScriptCache::get_full_script and once directly in Script::reload_from_file. This behavior existed before, and I didn’t change it.

I'm assuming you meant: "The only reason the script is already loaded here is to allow editing it outside the editor, without being connected to the LSP server."

Indeed, good catch. However, I removed that comment in the new version and created a function to check if the script needs to be reloaded: EditorFileSystem::_should_reload_script.


I also found a bug where adding or removing exported properties in tool scripts doesn't update live when the script is saved. You need to select another node and then reselect the node to refresh the property list in the inspector. I tested this without the PR, and the same thing happens. I'll create a separate issue for that.

@Hilderin Hilderin force-pushed the fix-reloading-scripts-already-in-use branch from a42d379 to 019ed62 Compare September 19, 2024 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ERROR: Condition "!p_keep_state && has_instances" is true. Returning: ERR_ALREADY_IN_USE
4 participants