From 5273084f9995572867631173e4a252ece2f28824 Mon Sep 17 00:00:00 2001 From: copygirl Date: Tue, 5 Mar 2024 09:25:04 +0100 Subject: [PATCH] Add doc comments for Entity.get / getRef / getMut --- src/entity.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/entity.zig b/src/entity.zig index 5517968..cadeeeb 100644 --- a/src/entity.zig +++ b/src/entity.zig @@ -301,16 +301,34 @@ pub fn Entity(comptime ctx: anytype) type { c.ecs_remove_id(self.world.raw, self.raw, ecs_id); } + /// Gets a value copy of a component. + /// If the component does not exist, returns null. pub fn get(self: Self, comptime T: type) ?T { return if (getRef(self, T)) |p| p.* else null; } + /// Gets an immutable pointer to a component. + /// If the component does not exist, returns null. + /// + /// The returned pointer may become invalid after calling other Flecs + /// functions, notably when an `Entity` moves to another table caused + /// by adding or removing other components. pub fn getRef(self: Self, comptime T: type) ?*const T { const id = Context.lookup(T).*; const ptr = c.ecs_get_id(self.world.raw, self.raw, id); return @alignCast(@ptrCast(ptr)); } + /// Get a mutable pointer to a component. + /// If the component did not yet exist, it will be added. + /// + /// The returned pointer may become invalid after calling other Flecs + /// functions, notably when an `Entity` moves to another table caused + /// by adding or removing other components. + /// + /// If `getMut` is called when the world is in deferred / readonly + /// mode, and the component does not yet exist, it will return a + /// pointer to a temp storage. pub fn getMut(self: Self, comptime T: type) *T { const id = Context.lookup(T).*; const ptr = c.ecs_get_mut_id(self.world.raw, self.raw, id);