From 3dec2867e5071dadc11f0a2c9160bcb35832926a Mon Sep 17 00:00:00 2001 From: abux Date: Mon, 13 Jul 2026 19:17:25 +0100 Subject: [PATCH] Added hasComponent | Forgor about it --- src/example/simple/component.zig | 2 ++ src/example/simple/system.zig | 18 +++++++++++++++++- src/world/world.zig | 10 ++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/example/simple/component.zig b/src/example/simple/component.zig index 98c1c84..c10ab4a 100644 --- a/src/example/simple/component.zig +++ b/src/example/simple/component.zig @@ -41,3 +41,5 @@ pub const Player = struct { try writer.print("\"{s}\"", .{self.name}); } }; + +pub const Dead = struct { value: bool = false }; diff --git a/src/example/simple/system.zig b/src/example/simple/system.zig index 93aeb6a..d137ca5 100644 --- a/src/example/simple/system.zig +++ b/src/example/simple/system.zig @@ -153,7 +153,7 @@ pub fn test_ecs( ctx: App.scheduler.Context, ) !void { // --- SPAWN ENTITY --- - try ctx.app.world.spawn(.{ + const player = try ctx.app.world.spawnEntity(.{ component.Player{ .name = "abux" }, component.Material{}, component.Transform{}, @@ -204,4 +204,20 @@ pub fn test_ecs( ); std.debug.print("\x1b[0m", .{}); } + + if (ctx.app.world.getComponent(component.Dead, player)) |_| { + std.debug.print("------------------>\n", .{}); + } + + // try ctx.app.world.addComponents(player, .{component.Dead{}}); + try ctx.app.world.addComponents(player, .{ + component.Dead{}, + }); + + const cmps = try ctx.app.world.getComponents(struct { + mat: *component.Material, + dead: ?*component.Dead, + }, player); + + if (cmps.dead) |_| {} } diff --git a/src/world/world.zig b/src/world/world.zig index 8c281b1..d139d59 100644 --- a/src/world/world.zig +++ b/src/world/world.zig @@ -225,6 +225,16 @@ pub fn getComponents( return result; } +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn hasComponent( + self: *Self, + comptime T: type, + entity: u64, +) bool { + return self.getComponent(T, entity) != null; +} + /// ---------------------------------------------------- /// ---------------------------------------------------- pub fn despawn(