From aa96ef73ada5caf715286f55cb0578befa4fc14f Mon Sep 17 00:00:00 2001 From: copygirl Date: Sun, 7 Apr 2024 16:54:24 +0200 Subject: [PATCH] Set component names explicitly .. rather than guessing from type name, which can be pretty inaccurate at times. --- src/meta.zig | 8 -------- src/world.zig | 10 ++++------ 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/meta.zig b/src/meta.zig index 427bd2c..fab8461 100644 --- a/src/meta.zig +++ b/src/meta.zig @@ -85,11 +85,3 @@ pub fn AnyToType(comptime expr: anytype) type { else => @compileError("Expression must be a type or a tuple of two types"), } } - -/// Gets the simplified type name of the specified type. -/// That is, without any namespace qualifiers. -pub fn simpleTypeName(comptime T: type) [:0]const u8 { - const fullName = @typeName(T); - const index = std.mem.lastIndexOf(u8, fullName, "."); - return if (index) |i| fullName[(i + 1)..] else fullName; -} diff --git a/src/world.zig b/src/world.zig index 34fe256..df8f5fd 100644 --- a/src/world.zig +++ b/src/world.zig @@ -137,17 +137,15 @@ pub fn World(comptime ctx: anytype) type { return Entity.init(self, config, add); } - pub fn tag(self: *Self, comptime T: type) !Entity { + pub fn tag(self: *Self, name: [:0]const u8, comptime T: type) !Entity { if (@sizeOf(T) > 0) @compileError("'" ++ @typeName(T) ++ "' must be a zero-sized type"); - const name = meta.simpleTypeName(T); const result = try self.entity(.{ .name = name, .symbol = name }, .{}); Context.lookupMut(T).* = result.raw; return result; } - pub fn component(self: *Self, comptime T: type) !Entity { + pub fn component(self: *Self, name: [:0]const u8, comptime T: type) !Entity { if (@sizeOf(T) == 0) @compileError("'" ++ @typeName(T) ++ "' must not be a zero-sized type"); - const name = meta.simpleTypeName(T); const entity_ = try self.entity(.{ .name = name, .symbol = name, .use_low_id = true }, .{}); const result = c.ecs_component_init(self.raw, &.{ @@ -169,8 +167,8 @@ pub fn World(comptime ctx: anytype) type { /// Use `get()` and `set()` to get and set the value of this singleton. /// /// Returns the created component entity. - pub fn singleton(self: *Self, comptime T: type, value: T) !Entity { - const single = try component(self, T); + pub fn singleton(self: *Self, name: [:0]const u8, comptime T: type, value: T) !Entity { + const single = try component(self, name, T); single.set(T, value); return single; }