// HACKER NEWS — CYBERSECURITY
Using any C++ library in Godot
Godot has become one of the most popular game
engines of the last few years. It is free, open source under the MIT license,
and small enough to download and start using in minutes. Most Godot games are
written in GDScript, the engine’s own scripting language.
Sooner or later, though, many projects need something that already exists as
a C or C++ library: a simulation library, a database, a networking protocol,
a machine learning runtime. GDScript cannot call native code, but Godot can
load it through GDExtension, and godot-cpp, the official C++
bindings, lets you expose that code as regular engine classes. Writing the
C++ code is the easy part. The hard part is the build: godot-cpp has to match
your Godot version, and every library you add has to be compiled for each
platform you ship to.
In this post we give a short tour of Godot, explain how C++ extensions work,
and show how to bring C++ libraries into a Godot game with Conan and
godot-cpp 10, now available in ConanCenter. As an example we
will use flecs, an Entity
Component System library, to simulate 100,000 particles inside a Godot scene.
Godot is a general purpose engine for 2D and 3D games. Everything in a Godot
project is built from two concepts:
Behavior is usually added by attaching a script to a node. GDScript is a
Python-like language designed for the engine, and it is great for gameplay
logic because changes show up immediately without a compile step.
What makes Godot interesting for C++ developers is that the engine itself is
written in C++, and it can load extensions written in C++ without being
recompiled. A class that comes from one of these extensions becomes a regular
engine class: it shows up in the editor next to the built-in nodes, with its
properties in the Inspector, and GDScript can use it like any other node. The
next section explains how these extensions work.
GDExtension is the recommended approach for most projects, and it is how many
popular plugins are distributed today. Because the C interface is verbose to
use directly, the Godot team maintains
godot-cpp, a C++ library that
wraps it with an API very close to the one used inside the engine. It provides
a C++ class for every engine class, such as Node2D, Sprite2D or Input.
Your own classes are regular C++ code that derives from those classes. A node
written with godot-cpp looks like this:
Since version 10.0, a single godot-cpp release works with any Godot version
from 4.3 onwards. You pick one with the api_version build option, and
godot-cpp generates its C++ classes from the API of that version. An
extension built for Godot 4.3 also works in newer versions, but not in older
ones, so you usually pick the oldest Godot version you want to support.
There is one more concept you need to know before building anything.
godot-cpp is compiled for one of three targets, named after the Godot
builds that load the library: