Add World.getScope and .setScope functions

main
copygirl 1 year ago
parent 9faed20c54
commit 680852f4f8
  1. 4
      src/entity.zig
  2. 20
      src/world.zig

@ -142,8 +142,8 @@ pub fn Entity(comptime ctx: anytype) type {
// Set the scope to create the entity under (its parent). // Set the scope to create the entity under (its parent).
// This avoids using up an id for a `ChildOf` relationship. // This avoids using up an id for a `ChildOf` relationship.
const previous = if (scope) |s| c.ecs_set_scope(world.raw, s) else null; const previous = if (scope) |s| world.setScope(s) else null;
defer _ = if (previous) |s| c.ecs_set_scope(world.raw, s); defer _ = if (previous) |s| world.setScope(s);
// TODO: Use an allocator that's well-fitted for super short-lived allocations. // TODO: Use an allocator that's well-fitted for super short-lived allocations.
const nameZ = if (name) |n| try flecs.allocator.dupeZ(u8, n) else null; const nameZ = if (name) |n| try flecs.allocator.dupeZ(u8, n) else null;

@ -145,5 +145,25 @@ pub fn World(comptime ctx: anytype) type {
context.func(iter); context.func(iter);
} }
}; };
/// Gets the current scope of this `World`. See also: `setScope()`.
pub fn getScope(self: *Self) ?Entity(ctx) {
const result = c.ecs_get_scope(self.raw);
return if (result != 0) Entity(ctx).fromRaw(self, result) else null;
}
/// Sets the current scope of this `World` to the specified entity.
///
/// Setting a scope causes certain operations to be made in relation
/// to the scope entity, rather than the world root. Passing `null`
/// causes the scope to be set to the world root.
///
/// Returns the previously set scope, if any. It's recommended to set
/// the scope back to the previous value after you're done operating
/// in the desired scope.
pub fn setScope(self: *Self, value: anytype) ?Entity(ctx) {
const result = c.ecs_set_scope(self.raw, util.anyToEntity(ctx, value));
return if (result != 0) Entity(ctx).fromRaw(self, result) else null;
}
}; };
} }

Loading…
Cancel
Save