diff --git a/src/entity.zig b/src/entity.zig index 6e93dc3..cb0730d 100644 --- a/src/entity.zig +++ b/src/entity.zig @@ -142,8 +142,8 @@ pub fn Entity(comptime ctx: anytype) type { // Set the scope to create the entity under (its parent). // This avoids using up an id for a `ChildOf` relationship. - const previous = if (scope) |s| c.ecs_set_scope(world.raw, s) else null; - defer _ = if (previous) |s| c.ecs_set_scope(world.raw, s); + const previous = if (scope) |s| world.setScope(s) else null; + defer _ = if (previous) |s| world.setScope(s); // 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; diff --git a/src/world.zig b/src/world.zig index 077335c..f5d53a2 100644 --- a/src/world.zig +++ b/src/world.zig @@ -145,5 +145,25 @@ pub fn World(comptime ctx: anytype) type { 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; + } }; }