Upgrade C++ API to Godot 4.2

·

4 min read

Update on Hot-Reloading

First of all:

Hot-reloading is finally here! Yay! 🥳

With Godot 4.2 came that awesome feature which should speed things up if you want to use C++ as your main development language for your Godot project. For those who don't know what hot-reloading is and why it was an issue with the Godot game engine: Prior to Godot 4.2, it was not possible to compile your C++ GDExtension code while your project was open in Godot's editor. You had to close your project in order for the code to compile, then you had to re-open it as the shared libraries generated during compilation weren't automatically re-integrated into the project. This made developing in C++ with Godot tedious and much slower. As you can probably imagine, changes to game code occur very often during development. But this has been improved and that missing feature was finally integrated. Now you can compile your C++ code while the GDExtension is loaded in a project. Godot will automatically reload the freshly compiled libraries. (Note, however, that there are currently still some issues. I'll talk about them further down.)

Moving from Godot 4.1 to Godot 4.2

Note that this is not a full migration guide (as I don't have much to migrate yet). But there are important game-breaking changes mentioned in the official blog post related to the new release. Read about that here: https://godotengine.org/article/godot-4-2-arrives-in-style/#critical-and-breaking-changes

As I started out with Godot 4.1 and didn't really create any complex projects yet, I didn't have to do much to ensure that I can continue using C++ as my main development language for Godot.

First, I had to pull the newest changes for Godot's official C++ API from the corresponding repository (which is godot-cpp) and change the branch to 4.2. Using git, this can be done as easily as:

git pull
git checkout 4.2

Then, the C++ API had to be recompiled via:

scons platform=windows use_mingw=yes target=template_release -j8

The same command has to be executed again for the project itself. (Don't forget to change the parameters as needed. I've talked about them before in my other blog post about setting up C++ development with Godot.)

And, since I would like to use my nodes already within the editor, the debug build is required as well. (Alternatively, it is also possible to set the path of the debug-built library within the .gdextension file to the release build. But for development purposes, using the debug build should usually suffice and comes with the advantage of further debug symbols.) So, I just ran the command for both the C++ bindings and my project again with the debug target selected:

scons platform=windows use_mingw=yes target=template_debug -j8

With that, I am done with the upgrade. It was as simple as that. However, don't forget to check whether you need to take further steps for a safe migration by looking into the migration hints.

Enabling Hot-Reloading

I was eager to try the new hot reloading feature. In order to use it, we first need to enable it though. (This is currently not mentioned in the official documentation, but should come soon.) This is done by adding the parameter reloadable = true to the .gdextension file of the project right in the [configuration] section. In my example, it looks like this now:

[configuration]

entry_symbol = "example_library_init"
compatibility_minimum = "4.2"
reloadable = true

[libraries]

macos.debug = "res://bin/libgdexample.macos.template_debug.framework"
macos.release = "res://bin/libgdexample.macos.template_release.framework"
windows.debug.x86_32 = "res://bin/libgdexample.windows.template_debug.x86_32.dll"
windows.release.x86_32 = "res://bin/libgdexample.windows.template_release.x86_32.dll"
windows.debug.x86_64 = "res://bin/libgdexample.windows.template_debug.x86_64.dll"
windows.release.x86_64 = "res://bin/libgdexample.windows.template_release.x86_64.dll"
linux.debug.x86_64 = "res://bin/libgdexample.linux.template_debug.x86_64.so"
linux.release.x86_64 = "res://bin/libgdexample.linux.template_release.x86_64.so"
linux.debug.arm64 = "res://bin/libgdexample.linux.template_debug.arm64.so"
linux.release.arm64 = "res://bin/libgdexample.linux.template_release.arm64.so"
linux.debug.rv64 = "res://bin/libgdexample.linux.template_debug.rv64.so"
linux.release.rv64 = "res://bin/libgdexample.linux.template_release.rv64.so"
android.debug.x86_64 = "res://bin/libgdexample.android.template_debug.x86_64.so"
android.release.x86_64 = "res://bin/libgdexample.android.template_release.x86_64.so"
android.debug.arm64 = "res://bin/libgdexample.android.template_debug.arm64.so"
android.release.arm64 = "res://bin/libgdexample.android.template_release.arm64.so"

Now, everytime you recompile your changed game code (using the debug build at first, since this is the one that will be loaded into the project when you're in the editor, unless you've changed the paths as mentioned before) the changes should be reflected in Godot's editor without having to reload the project.

Well, this almost works. In fact, running the scene or game will use the changes you made in your code. But in my case those changes are not really shown in the editor viewport. For example, renaming a button via code will show up when running a scene, but it will not show in the editor. This can be fixed by reloading the project. However, this is only part of what I would expect from hot-reloading. So if you experience similar issues, reload the project for now in case you need those changes directly visible in the editor viewport. I've raised this as an issue, so let's see when and how this gets fixed.

That's it for now. My next step will be to make a first minimalistic 3D model of a tree in Blender and try to get it into a Godot project somehow. I'll write a blog post accordingly.

Until next time!

– Zacryon