Zig build package and bindings for https://github.com/SanderMertens/flecs
  • C 94.3%
  • Zig 5.7%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-10-21 17:56:49 +01:00
.github/workflows Update main.yml for Zig 0.17.0 requirement (#46) 2026-10-21 17:56:49 +01:00
libs/flecs Upgrade flecs, fix *const for some functions. 2026-03-16 14:50:56 +01:00
src Upgrading to zig 0.17.0 2026-10-10 14:17:27 -07:00
.gitignore Upgrading to zig 0.17.0 2026-10-10 14:17:27 -07:00
build.zig Upgrading to zig 0.17.0 2026-10-10 14:17:27 -07:00
build.zig.zon Upgrading to zig 0.17.0 2026-10-10 14:17:27 -07:00
LICENSE files from monorepo @ 613d847104a072fc1d4442f58d5f50b3f9c3f27c 2024-11-03 21:30:46 +00:00
README.md Upgrading to zig 0.17.0 2026-10-10 14:17:27 -07:00

zflecs

Zig build package and bindings for flecs ECS v4.1.5

Minimum Zig Version: 0.17.0

Getting started

Examplebuild.zig:

pub fn build(b: *std.Build) void {
    const exe = b.addExecutable(.{ ... });

    const zflecs = b.dependency("zflecs", .{});
    exe.root_module.addImport("zflecs", zflecs.module("root"));
    exe.root_module.linkLibrary(zflecs.artifact("flecs"));
}

Now in your code you may import and use zflecs:

const std = @import("std");
const ecs = @import("zflecs");

const Position = struct { x: f32, y: f32 };
const Velocity = struct { x: f32, y: f32 };
const Eats = struct {};
const Apples = struct {};

fn move_system(positions: []Position, velocities: []const Velocity) void {
    for (positions, velocities) |*p, v| {
        p.x += v.x;
        p.y += v.y;
    }
}

//Optionally, systems can receive the components iterator (usually not necessary)
fn move_system_with_it(it: *ecs.iter_t, positions: []Position, velocities: []const Velocity) void {
    const type_str = ecs.table_str(it.world, it.table).?;
    std.debug.print("Move entities with [{s}]\n", .{type_str});
    defer ecs.os.free(type_str);

    for (positions, velocities) |*p, v| {
        p.x += v.x;
        p.y += v.y;
    }
}

pub fn main(init : std.process.Init.Minimal) !void {
    _ = init;
    const world = ecs.init();
    defer _ = ecs.fini(world);

    ecs.COMPONENT(world, Position);
    ecs.COMPONENT(world, Velocity);

    ecs.TAG(world, Eats);
    ecs.TAG(world, Apples);

    _ = ecs.ADD_SYSTEM(world, "move system", ecs.OnUpdate, move_system);
    _ = ecs.ADD_SYSTEM(world, "move system with iterator", ecs.OnUpdate, move_system_with_it);

    const bob = ecs.new_entity(world, "Bob");
    _ = ecs.set(world, bob, Position, .{ .x = 0, .y = 0 });
    _ = ecs.set(world, bob, Velocity, .{ .x = 1, .y = 2 });
    ecs.add_pair(world, bob, ecs.id(Eats), ecs.id(Apples));

    _ = ecs.progress(world, 0);
    _ = ecs.progress(world, 0);

    const p = ecs.get(world, bob, Position).?;
    std.debug.print("Bob's position is ({d}, {d})\n", .{ p.x, p.y });
}

zig build run should result in:

Move entities with [main.Position, main.Velocity, (Identifier,Name), (main.Eats,main.Apples)]
Move entities with [main.Position, main.Velocity, (Identifier,Name), (main.Eats,main.Apples)]
Bob's position is (4, 8)