godot-cpp
repository·master·Indexed 25 days ago
https://github.com/godotengine/godot-cppC++ bindings for the Godot Engine's GDExtension API, enabling the development of high-performance engine extensions. Includes C header files, API JSON definitions, and guidance on targeting specific Godot API versions, configuring .gdextension files, implementing entry points, and registering classes in the Godot ClassDB.
What's inside godot-cpp
- This repository provides the C header files and the API JSON required to interface with Godot Engine via the GDExtension system. These files define the interface used to write high-performance C/C++ extensions for Godot.
Sync GDExtension header and API with a custom Godot build
masterIf you are using a modified version of Godot or a specific commit that is not covered by the current repository version, you can manually generate the required
extension_api.jsonandgdextension_interface.jsonfiles.- Compile Godot Engine at your desired version/commit, or download the official release executable.
- Run the executable with the specific flags to dump the API definitions.
godot --dump-extension-api --dump-gdextension-interface-jsonSet the target Godot API version
masterWhen building
godot-cpp, you can specify which Godot version your GDExtension targets. GDExtensions targeting an earlier version of Godot should work in later minor versions, but not vice-versa.You can specify the version using the
api_versionSCons option, or by providing a customextension_api.jsonfile generated from your specific Godot version.# Target a specific Godot version scons api_version=4.3 # Target using a custom API JSON file generated by your Godot version godot --dump-extension-api scons custom_api_file=extension_api.jsonUse godot-cpp templates and examples
masterFor a complete starting point, use thegodot-cpp-templateproject. You can also refer to theSummatorexample for a concrete implementation of a GDExtension.Configure a .gdextension file
masterTo use your compiled shared library in a Godot project, you must create a
.gdextensionconfiguration file. This file maps platform-specific library paths to the engine. Theentry_symbolmust match the name of your C-style initialization function.[configuration] entry_symbol = "example_library_init" compatibility_minimum = "4.1" [libraries] macos.debug = "res://bin/libgdexample.macos.debug.framework" macos.release = "res://bin/libgdexample.macos.release.framework" windows.debug.x86_64 = "res://bin/libgdexample.windows.debug.x86_64.dll" windows.release.x86_64 = "res://bin/libgdexample.windows.release.x86_64.dll" linux.debug.x86_64 = "res://bin/libgdexample.linux.debug.x86_64.so" linux.release.x86_64 = "res://bin/libgdexample.linux.release.x86_64.so"Implement the GDExtension entry point
masterYour library must export a C function (using
extern "C") that serves as the entry point. This function usesgodot::GDExtensionBinding::InitObjectto initialize the binding, register initialization/termination callbacks, and set the minimum initialization level.extern "C" { // Initialization. GDExtensionBool GDE_EXPORT example_library_init(GDExtensionInterfaceGetProcAddress p_get_proc_address, GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) { godot::GDExtensionBinding::InitObject init_obj(p_get_proc_address, p_library, r_initialization); init_obj.register_initializer(initialize_example_module); init_obj.register_terminator(uninitialize_example_module); init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE); return init_obj.init(); } }Register classes in the Godot ClassDB
masterDuring the initialization phase, you must register your C++ classes so they are available to Godot's engine and scripting systems. Use the
GDREGISTER_CLASSmacro within your initialization function, ensuring you check for the correctModuleInitializationLevel.using namespace godot; void initialize_example_module(ModuleInitializationLevel p_level) { if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { return; } GDREGISTER_CLASS(Example); }