godot-rust / gdext

Rust bindings for Godot 4
https://mastodon.gamedev.place/@GodotRust
Mozilla Public License 2.0
3.06k stars 190 forks source link

Rust compilation requires Godot editor restart #1

Closed Bromeon closed 1 year ago

Bromeon commented 2 years ago

Upstream issue: https://github.com/godotengine/godot/issues/66231

On Windows, the Godot editor "locks" a DLL containing a GDExtension library and releases it only after shutdown. Native code can thus not be recompiled as long as the editor remains open. Behavior on Linux and Mac is unclear (please let me know about your experiences).

This is a considerable limitation for game developers using GDExtension. Requiring the user to reload the editor on every change not only makes a very common workflow impossible, but is also a regression from GDNative, where reloading (for non-tool classes) could be achieved while the editor was out of focus -- even if it had its bugs.

We depend on Godot to provide a mechanism that allows dynamic libraries to be recompiled. Even without a full-featured hot reloading, a bit part of the problem could be alleviated by using the recompiled dynamic library for the launched application (not in-editor).

AckslD commented 1 year ago

On linux I can rebuild the DLL fine but some things are not updated. For example I've seen that I need to restart godot (maybe there is another way to trigger a "refresh"?) when:

setzer22 commented 1 year ago

I am also seeing a very similar behavior on Linux. It makes sense because:

So, basically, reloading the game changes happens to work on Linux merely by accident. It was not intended in the design GDExtension. That's also why in Windows, the DLL is being locked and never freed, because the process does not expect it to ever change. It is generally a reasonable assumption to make for plugin development, but seriously hampers game development 😕

AckslD commented 1 year ago

Btw, to reload signals etc from the DLL, it does work to do Project -> Reload current project :+1: I guess it's effectively the same as restarting godot but a bit more convenient. Also closing to project menu and opening the project again works.

sadovsf commented 1 year ago

I can confirm similar behavior on osx as well. I can leave editor open and recompile at will as long as iam not adding new end points essentially. In that case for editor to notice them project reload must be used.

jcdickinson commented 1 year ago

On Windows it is usually possible to move a locked file (so long as it remains on the same disk), which would allow the build to proceed. I no longer use Windows, so I can't confirm whether this would work in this case.

If it does, you only need to move the file every time the editor starts. You could probably attempt to acquire a read lock in build.rs, or powershell, or something and move the file if that fails.

Bromeon commented 1 year ago

The thing is that all GDExtension bindings currently suffer from this issue, so I'd rather not build a Rust-specific workaround.

It would be nice if this were fixed in Godot itself. https://github.com/godotengine/godot-cpp/issues/955 proposes an approach, but priorities have shifted again... I'll try to bring this up again 🙂

Bromeon commented 1 year ago

A fix for Windows DLLs has been merged in https://github.com/godotengine/godot/pull/80188! Now all three main platforms allow to recompile Rust while the editor is open, which is really nice 😊

In other great news, an initial step towards hot reloading is being worked on in https://github.com/godotengine/godot/pull/80284 🚀

LeaoLuciano commented 1 year ago

Testing https://github.com/godotengine/godot/pull/80284 and changing only compat things in gdext (https://github.com/LeaoLuciano/gdext/tree/hot-reload), it works (on linux):

https://github.com/godot-rust/gdext/assets/56130740/85e549fd-ec03-4129-8cbb-e1ef0c8b715a

Bromeon commented 1 year ago

Both

have been merged to Godot master. We can consider this very first issue and big blocker resolved! 🚀

There will likely be some follow-up questions and improvements on hot reloading, but these can be addressed separately 🙂