Godot C++ GDExtension Setup With CMake
What is GDExtension?
GDExtension is a Godot-specific technology that allows the Godot engine to interact with native shared libraries at runtime.
Basically, it allows you to write and compile code in C++ that works seamlessly with Godot. (Note there’s another way to do this, but GDExtension is what I’m using to make my game)
Getting Started
There is a page on the Godot Docs that explains how to get setup for GDExtension development
- This is fine, and you can definitely follow that and get a solid setup
- But I’m going to show you a setup that has several advantages:
- Uses cmake rather than scons, beause:
- I found SCons to be annoying to modify
- cmake is at least more ubiquitous so it’s been easy for me to extend
- This repo has good github action templates, which can be used to get CI/CD setup
- It’s better, trust me!
Prerequisites
- cmake version 3.22+
- C++ compiler
Steps:
Setup the Repo:
First clone the repository.
git clone https://github.com/asmaloney/GDExtensionTemplate.git game
cd game
This repo is just a template to work off of, starting point, so we don’t want to keep any of the old git information around.
To do this we can just delete the .git folder, then reinitialize the git repo
rm -rf .git
git init
Now delete some files that we don’t need (for now)
rm README.md
rm CHANGELOG.md
rm LICENSE.md
Now we have to add back the submodule for the Godot cpp gdextension bindings. This repository is setup for godot 4.2, if you are using a different godot version you’ll need to check out a different version of this repository.
Here I’ll check out the git tag matching my godot version: 4.2.2
rm -r extern/godot-cpp
git submodule add https://github.com/godotengine/godot-cpp.git extern/godot-cpp
cd extern/godot-cpp
git checkout godot-4.2.2-stable
cd ../..
We also should delete or disable the github actions for this repo. Don’t worry, in a later video, i’ll show you how to use these to setup a build server for your game.
mv .github .github_disabled
or
rm -rf .github
Now initialize our repository.
git add .
git commit -m "Initial commit"
Setup the godot project structure
Now let’s set up the godot project and modify some things so that our C++ library gets built into a location that godot can use
mkdir game
mkdir game/bin
Go to godot, create the project in this folder.
Now edit the CMakeLists.txt
in the project root
Make the following changes:
Change the project name:
# Main project information
project( game
LANGUAGES
CXX
VERSION
0.1.0
)
Change the build output directory. This step puts the binaries generated from our C++ code in a place that godot can use. We are putting them in the game/bin
directory that we just created.
# BUILD_OUTPUT_DIR is where we put the resulting library (in the build directory)
set( BUILD_OUTPUT_DIR "${PROJECT_BINARY_DIR}/../${PROJECT_NAME}/bin/" )
Now edit the templates/CMakeLists.txt
file.
Change the output directory for the .gdextension file. This file is needed for godot to recognize our extension.
# Generate our project's .gdextension file from the template
set( GD_EXTENSION_FILE ${PROJECT_NAME}.gdextension )
configure_file( ${GD_EXTENSION_FILE_INPUT} ${PROJECT_BINARY_DIR}/../${PROJECT_NAME}/bin/${GD_EXTENSION_FILE} )
Add to the .gitignore
Add the following lines to your .gitignore
so that our build files are ignored from source control
build/
install/
.cache/
Build
Now, it’s time to build. We are going to:
- create a build directory
- create a directory for “install” the final builds into (not needed for now, but can be useful later)
- setup the cmake build directory
- build the project
The initial build will probably take quite a while since we are building the godot-cpp bindings, but subsequent builds will be quick since you don’t need to recompile all that code.
mkdir build
mkdir install
cmake -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=install
cmake --build build --parallel
Done - Test
Now we are done with setup, we can go into godot and we can see the example nodes are available to choose from the editor. Note that we don’t need to attach scripts or anything to these nodes, we are essentially creating custom node types that are available for use within the Godot editor.
You are now setup to use C++ with GDExtensions.