Skip to content

gdzig/gdzig

Repository files navigation

gdzig

Idiomatic Zig bindings for Godot 4.

DISCLAIMER

This library is currently undergoing rapid development and refactoring as we figure out the best API to expose. Bugs and missing features are expected until a stable version is released. Issue reports, feature requests, and pull requests are all very welcome.

Prerequisites

  1. zig 0.15.1+
  2. godot 4.4+

Note: We are targeting stable releases of Zig only. 0.16.x is not currently supported.

Usage:

See the example folder for reference.

Code Sample:

const GuiNode = @This();
pub fn register(r: *Registry) void {
const class = r.createClass(GuiNode, r.allocator, .auto);
class.addMethod("on_pressed", .auto);
class.addMethod("on_toggled", .auto);
}
allocator: Allocator,
base: *Control,
sprite: *Sprite2D = undefined,
pub fn create(allocator: *Allocator) !*GuiNode {
const self = try allocator.create(GuiNode);
self.* = .{
.allocator = allocator.*,
.base = Control.init(),
};
self.base.setInstance(GuiNode, self);
return self;
}
pub fn destroy(self: *GuiNode, allocator: *Allocator) void {
self.base.destroy();
allocator.destroy(self);
}
pub fn _enterTree(self: *GuiNode) void {
if (Engine.isEditorHint()) return;
var normal_btn = Button.init();
self.base.addChild(.upcast(normal_btn), .{});
normal_btn.setPosition(Vector2.initXY(100, 20), .{});
normal_btn.setSize(Vector2.initXY(100, 50), .{});
normal_btn.setText(.fromLatin1("Press Me"));
var toggle_btn = CheckBox.init();
self.base.addChild(.upcast(toggle_btn), .{});
toggle_btn.setPosition(.initXY(320, 20), .{});
toggle_btn.setSize(.initXY(100, 50), .{});
toggle_btn.setText(.fromLatin1("Toggle Me"));
toggle_btn.connect(Button.Toggled, .fromClosure(self, &onToggled)) catch {};
normal_btn.connect(Button.Pressed, .fromClosure(self, &onPressed)) catch {};
var res_name: String = .fromLatin1("res://textures/logo.png");
defer res_name.deinit();
const texture = ResourceLoader.load(res_name, .{}).?;
defer if (texture.unreference()) texture.destroy();
self.sprite = Sprite2D.init();
self.sprite.setTexture(Texture2D.downcast(texture).?);
self.sprite.setPosition(.initXY(400, 300));
self.sprite.setScale(.initXY(0.6, 0.6));
self.base.addChild(.upcast(self.sprite), .{});
}

Community

Find us in the gdzig Discord server.

About

Zig bindings for Godot 4

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 16

Languages