Idiomatic Zig bindings for Godot 4.
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.
- zig 0.15.1+
- godot 4.4+
Note: We are targeting stable releases of Zig only. 0.16.x is not currently supported.
See the example folder for reference.
Lines 1 to 56 in 1cdfec6
| 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), .{}); | |
| } |
Find us in the gdzig Discord server.