Building Godot 4 GDExtension C++ example with Docker

For the past couple years I have been tinkering with the Godot Engine which I absolutely love. The only issue which is completely unrelated to the engine is that I cannot find the discipline to finish a project so far.

Beside the fact that this engine is truly amazing, I was reading about its C++ GDExtension documentation and it grabbed my curiosity.

I ended up making this repository to make things simpler and avoid cluttering my operating system. All my development tools are dockerized on my Linux system and I also wanted to try it on Windows 10 without installing Python along with a full toolchain.

Requirements

The only requirements you need on both Windows and Linux are:

Instructions

The instructions are actually pretty simple, if you are trying to do this on Windows however just make sure you run all commands from a Git Bash prompt.

Clone this repository and build the docker image:

git clone https://github.com/mrt-prodz/docker-godot-gdextension-builder
cd docker-godot-gdextension-builder
docker compose build

This docker image will contain the following softwares:

Clone Godot 4.2 stable branch and initialize the submodules:

git clone -b 4.2 https://github.com/godotengine/godot-cpp
cd godot-cpp
git submodule update --init
cd ..

That's it, if the docker image built properly you should have all the required tools to compile the GDExtension using the C++ bindings for the Godot Engine's GDExtensions API.

The source files in the src directory of this repository are coming from the Godot 4.2 documentation. It randomly move the position of the Node:

void GDExample::_process(double delta) {
time_passed += delta;

Vector2 new_position = Vector2(10.0 + (10.0 * sin(time_passed * 2.0)), 10.0 + (10.0 * cos(time_passed * 1.5)));

set_position(new_position);
}

Building the GDExtension

You should now be able to build by just running the following command:

./build.sh

If your operating system is not detected automatically, you can still run one of the following command manually:

Windows

winpty docker run --rm -it -v /$(pwd):/mnt mrt-prodz/godot-gdextension-builder scons platform=windows -j4

Linux

docker run --rm -it -v /etc/passwd:/etc/passwd:ro -v /etc/group:/etc/group:ro -u $(id -u):$(id -g) -v ${PWD}:/mnt mrt-prodz/godot-gdextension-builder scons platform=linux -j4

The first build will take some time but it should eventually succeed. I tested this on Windows 10 and Fedora 38.

Using your new extension

In order to use your newly built GDExtension you need to run a version of Godot equal or higher than the repository version you cloned.

For this repository, open Godot 4.2, create a project in the demo folder and you should see the GDExtension loading automatically.

Create a new 2D Scene, press CTRL + a then add a GDExample Node. You should see the position of the Node randomly moving.

If everything worked properly, you can now start to program and build your very own GDExtension in C++ for the Godot Engine.

Note

The repository actually use Godot 4.2 stable branch but if you would like to use a more recent version you will have to change 4.2 with another branch when cloning the godot-cpp repository.

Simply change the version in the following command:

git clone -b 4.x https://github.com/godotengine/godot-cpp

The GDExtension example in this repository is coming from the Godot 4.2 documentation. There is no Engine.is_editor_hint() check in the process method in order to avoid running the code inside the Godot editor. Keep this in mind if you start experimenting with heavy calculations to avoid crashing the editor.

Repository

You can find the source on github: github.com/mrt-prodz/docker-godot-gdextension-builder