Add lookupSymbol, lookupChild, rename lookupPath

main
copygirl 9 months ago
parent 96e511bfc1
commit ae34763f71
  1. 21
      src/world.zig

@ -59,13 +59,32 @@ pub fn World(comptime ctx: anytype) type {
return result; return result;
} }
/// Returns the `Entity` with the specified unique symbol, or null if none.
pub fn lookupSymbol(self: *Self, symbol: []const u8) ?Entity {
const result = c.ecs_lookup_symbol(self.raw, symbol.ptr, false, false);
return if (result != 0) Entity.fromRaw(self, result) else null;
}
/// Returns the `Entity` with the specified parent and name, or null if none.
/// If no parent is specified, the world root is used instead.
pub fn lookupChild(self: *Self, parent: anytype, name: []const u8) ?Entity {
var name_buffer: [256]u8 = undefined;
if (name.len + 1 > name_buffer.len) @panic("Name too long");
@memcpy(name_buffer[0..name.len], name);
name_buffer[name.len] = 0;
const parent_ = Context.anyToEntity(parent);
const result = c.ecs_lookup_child(self.raw, parent_, &name_buffer);
return if (result != 0) Entity.fromRaw(self, result) else null;
}
// TODO: Reconsider whether lookup functions should return errors or just optionals. // TODO: Reconsider whether lookup functions should return errors or just optionals.
// TODO: Reconsider whether "Entity" should always be in backticks in our doc comments. // TODO: Reconsider whether "Entity" should always be in backticks in our doc comments.
// TODO: We really need tests for this function. // TODO: We really need tests for this function.
/// Returns the `Entity` at the specified path, or an error if the /// Returns the `Entity` at the specified path, or an error if the
/// entity does not exist. If the path is not absolute, the operation /// entity does not exist. If the path is not absolute, the operation
/// will use the current scope, or the world root. /// will use the current scope, or the world root.
pub fn lookupByPath(self: *Self, path: Path) !Entity { pub fn lookupPath(self: *Self, path: Path) !Entity {
var parent = if (path.absolute) 0 else c.ecs_get_scope(self.raw); var parent = if (path.absolute) 0 else c.ecs_get_scope(self.raw);
var current: c.ecs_entity_t = undefined; var current: c.ecs_entity_t = undefined;
for (path.parts) |part| { for (path.parts) |part| {

Loading…
Cancel
Save