Set component names explicitly

.. rather than guessing from type name,
which can be pretty inaccurate at times.
main
copygirl 7 months ago
parent 5425ba89d5
commit aa96ef73ad
  1. 8
      src/meta.zig
  2. 10
      src/world.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"), 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;
}

@ -137,17 +137,15 @@ pub fn World(comptime ctx: anytype) type {
return Entity.init(self, config, add); 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"); 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 }, .{}); const result = try self.entity(.{ .name = name, .symbol = name }, .{});
Context.lookupMut(T).* = result.raw; Context.lookupMut(T).* = result.raw;
return result; 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"); 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 entity_ = try self.entity(.{ .name = name, .symbol = name, .use_low_id = true }, .{});
const result = c.ecs_component_init(self.raw, &.{ 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. /// Use `get()` and `set()` to get and set the value of this singleton.
/// ///
/// Returns the created component entity. /// Returns the created component entity.
pub fn singleton(self: *Self, comptime T: type, value: T) !Entity { pub fn singleton(self: *Self, name: [:0]const u8, comptime T: type, value: T) !Entity {
const single = try component(self, T); const single = try component(self, name, T);
single.set(T, value); single.set(T, value);
return single; return single;
} }

Loading…
Cancel
Save