Example
GOC provides macros that guide the code generator and allows you to expose Godot Object derived classes and their properties, methods and signals to the engine.
Classes
#include "godot_object_compiler/macros.h"
#include "example_node.generated.h"
GODOT_CLASS();
class ExampleNode : public Node3D {
GODOT_GENERATED_BODY();ş
};
A class can be marked as a godot class so it is considered by the generators. Within the class body we add a generated body macro. This hook is used by the GOC to inject definitions into the class, such as getters and setters and other additional methods.
Check out GODOT_CLASS and GODOT_GENERATED_BODY for more information.
Properties
Next we might want to expose a bunch of properties. If needed we can always provide our own property hints and property usages within the macro parameters.
GODOT_PROPERTY(HintGlobalDir());
String global_path;
GODOT_PROPERTY(HintRange("0.0,1.0,0.01"));
float range;
Note
GOC provides convenience functions for all possible hint and usage values parsed directly from the godot-cpp library your linking to.
While properties are always public within the engine, we can modify the access specifier of the generated getters and setters for access within our extension.
GODOT_PROPERTY(PublicGet, PrivateSet);
int cpp_private_property;
It is also possible to expose Godot Object types and typed collections.
GODOT_PROPERTY();
TypedDictionary<int, Texture2D> textures;
GODOT_PROPERTY();
Ref<Texture2D> texture;
GODOT_PROPERTY();
Node3D *target_node = nullptr;
Check out GODOT_PROPERTY for more information.
Signals
If we want to add a signal we add a void method definition and mark it as a signal. This will register the signal with the appropriate method signature and generate an implementation for this method which can be used to emit the signal.
GODOT_SIGNAL();
void example_signal(int p_param);
Functions
GOC can also expose regular functions to the engine, and script virtual functions can be bound with a simple tag.
GODOT_FUNCTION();
Node *exposed_function();
GODOT_FUNCTION(ScriptVirtual);
int virtual_function(Node *p_param);
GODOT_FUNCTION();
static int static_function();
Check out GODOT_SIGNAL for more information.
Enums
Lets say we want to add a flags property to our node. We can add a marked enum to the class body, and specify that we would like this enum to be treated as flags in the macro parameters.
GODOT_ENUM(EnumFlags);
enum ExampleFlags {
FLAG_A = 1 << 0,
FLAG_B = 1 << 1,
FLAG_C = 1 << 2,
};
Next we add the property. The GOC queries the enum type and generates appropriate code to expose this property with the property hint to bind the enum names and values.
GODOT_PROPERTY();
ExampleFlags flags = FLAG_A;
Last but not least we add a generated global macro outside the class body. GOC uses this hook to generate code that needs to be added in the global namespace such as the enum variant cast macros.
GODOT_GENERATED_GLOBAL();
Check out GODOT_ENUM and GODOT_GENERATED_GLOBAL for more information.
Usage
GOC currently ships with integrations for CMake. The tools can be dumped into a local folder by calling the GOC executables init_tools program with a local path argument.
$ goc init_tools tools
CMake Integration
Include the tools file in your CMakeLists.txt and activate the GOC generator for your GDExtension target by specifying the target name the and sources root directory. GOC will then generate the bindings code for your targets source files.
#using example paths
include(tools/autogoc.cmake)
target_autogoc(${TARGET} src)
Check out CMake for more information.
SConstruct Integration
tbd
Check out SConstruct for more information.
Command Line Usage
You can also use the GOC as a CLI tool to manually generate the sources or build your own integrations. Execute
$ goc help
To show usage info or consult the Using the CLI documentation.