Zigodot: Godot Extension but it's a template

Zigodot: Godot Extension in zig but it’s a template…

Zigodot
Godot 4.7.2+ is Required.

This is not a typical zig pkg… it’s not supposed to be used with zig fetch --save.
It’s a template, clone it. And

zig build template -Doutdir=/path/to/<godot-project>/zig -Dname=MyGame

and build with

cd /path/to/<godot-project>/zig
zig build gen
zig build --prefix ../extensions/<name>

An example for general class usecase TestNode.zig.

Simplified TestNode.zig:

pub const Extends = godot.classes.core.Node;
const TestNode = @This();

unannotated: u8 = 67,
the_name: godot.annotations.Property(godot.builtins.String, .{
    .usage = .exported,
    .set = .best_effort,
    .deinit = .best_effort,
}).Init(.fromLatin1, .{"TheName2!"}),
base: godot.annotations.Base(Extends) = .{},
child_node: godot.annotations.OnReady(godot.classes.core.Node)
            .Node("./ChildNode"),
label: godot.annotations.OnReady(godot.builtins.String)
            .Call(.fromLatin1, .{"TestingString..."}),

pub const HealthChanged = godot.annotations.Signal("health_changed", .{
    .{ "health", i32 },
    .{ "source", ?*godot.classes.core.Node },
});

pub const OnInit = godot.annotations.InitCallback(TestNode, onInit);
pub const OnDeinit = godot.annotations.DeinitCallback(TestNode, onDeinit);

pub fn onInit(self: *@This(), allocator: std.mem.Allocator, base: *Extends) !void {}
pub fn onDeinit(self: *@This(), allocator: std.mem.Allocator) void {}

pub fn _physicsProcess(self: *TestNode, delta: f64) void {}

Why it’s a template? I wanted to expose an utility which would scan the source-directory to find registerable godot gdextension classes.
And something like this can be done:

const scan = b.step("scan", "Generate the extension class namespace module.");
const runtime_classes_scan = class_files.scanAndGenerate(b, "src", .{
    .generated_module_path = "src/example.runtime.gen.zig",
    .ignores = &.{ "godot/**", "**/*.gen.zig", "*Editor*.zig", "*Plugin.zig" },
});
scan.dependOn(&runtime_classes_scan.step);

const godot_build = @import("build/godot.zig");

So, this will scan the ./src folder for any .zig file which might contain a “registerable type”. And generate a <specified_name>.gen.zig file which then can be used this way:

godot.registry().addClasses(
    .{example_runtime},
    .{},
    .{ .instance_allocator = godot.engine_allocator },
) catch ...;
const example_runtime = @import("example.runtime.gen.zig");

And it builds a naive cache too based on the simple mtime/inode metadata along with the provided ingores/patterns arguments and the search/scan directory.

One caveat is now there’s a src/godot directory visible, but it gives users full control over all the “auto” layer, conversions too. They can modify and turn into their custom template..

I tried to design it as much explicit as possible. There’s no “true” convention. Except some features needs a field to be marked “Base” or typed Base(T). And has to expose pub const Extends = ...;.

These are a little convention had to follow: pub const Extends = <..>.

pub const Extends = godot.classes.core.Node;
const TestNode = @This();
// Normal zig field, not exposed to GDScript or Godot Engine...
unannotated: u8 = 67,
// Exposed to GDScript or Godot Engine...
the_name: godot.annotations.Property(godot.builtins.String, .{
    .usage = .exported,
    .set = .best_effort, // explicit wheather some "automatic" release on set is desired
    .deinit = .best_effort, // same "automatic" release, mainly works for godot engine classes.
// And classes which has an exported decleration typed
// godot.annotations.Conversion:
// https://github.com/IamSanjid/zigodot/blob/4e8cc5bd813aad7f8cda42c467a2ede2c509a8703e1c61e57aa2449537a40a88/src/godot/annotations/conversion.zig#L19
}).Init(.fromLatin1, .{"TheName2!"}), // godot.builtins.String.fromLatin1 gets called, 
// after the egine creates TestNode instance.
// but any custom method can be used here, but it has to accept 
// (*Owner) and return ?T.
base: godot.annotations.Base(Extends) = .{}, // The base.. the name doesn't matter,
// as long as the type is godot.annotations.Base(Extends)
// some of the features require a field like this, it's a borrow nothing should
// free whatever the inside value is, engine garuntees it's lifetime...
child_node: godot.annotations.OnReady(godot.classes.core.Node).Node("./ChildNode"),
// OnReady functions/callbacks gets called when `notification_post_enter_tree`
// is received and for the node it was received is not "ready" at that point,
// basically before "_ready", I just found this place is a great place to call
// OnReady hooks, utilizes Object.notification(instance, what, reversed) "callback".
// OnReady.Call is same as Property(T).Init, `.method` or any custom method the
// next parameter is basically "args" formatting, some constant values can be
// passed directly, to know more:
// https://github.com/IamSanjid/zigodot/blob/4e8cc5bd813aad7f8cda42c467a2ede2c509a8703e1c61e57aa2449537a40a88/src/godot/annotations/init.zig#L20
label: godot.annotations.OnReady(godot.builtins.String).Call(.fromLatin1, .{"TestingString..."}),
// Signal, "health_changed" is the engine exposed name, and next are pair
// arg_name and their types, only scalar, godot engine types
// (exposed through bindings/codegen), or a type which has
// pub exported declaration typed `godot.annotations.Conversion(..)`
pub const HealthChanged = godot.annotations.Signal("health_changed", .{
    .{ "health", i32 },
    .{ "source", ?*godot.classes.core.Node },
});
// There's no init/deinit convention you declare a "marker"
pub const OnInit = godot.annotations.InitCallback(TestNode, onInit);
pub const OnDeinit = godot.annotations.DeinitCallback(TestNode, onDeinit);

pub fn onInit(self: *@This(), allocator: std.mem.Allocator, base: *Extends) !void {
}

pub fn onDeinit(self: *@This(), allocator: std.mem.Allocator) void {
}
// for virtual function overrides, checks camelCase matched method declerations. like 
// _physics_process if a _physicsProcess pub declared function is there
// the Registry will try to register that method as a virtual function override.
// But there's also godot.annotations.Override("<engine_name>", theMethod) marker too!
// https://github.com/IamSanjid/zigodot/blob/4e8cc5bd813aad7f8cda42c467a2ede2c509a8703e1c61e57aa2449537a40a88/src/godot/annotations/override.zig#L9
pub fn _physicsProcess(self: *TestNode, delta: f64) void {}

Supported Zig versions

Zig-master… last checked with commit 658f7b0ea

AI / LLM usage disclosure

Docs/Comments were generated by LLM, tried to humanize as much as possible. The binding generator was also written in that way, but the structure and what/how the bindings should be generated was decided step by step by hand. Everything else every step/design decision was hand-checked.