A component is something that is added to an entity. Components can tag an entity (“this entity is an Npc”), attach data to an entity (“this entity is at Position Vector3.new(10, 20, 30)”), and create relationships between entities.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Ukendio/jecs/llms.txt
Use this file to discover all available pages before exploring further.
Components as Storage Keys
Components are keys to ECS storage columns. Think of the ECS storage as a table where:- Rows are entities
- Columns are components
- Cells are the component data for that entity
The component ID is just a number, but it’s been registered with the world. This registration allows the ECS to know about the component and allocate storage for it when entities use it.
Creating Components
Create a component usingworld:component() with a type annotation:
Setting Component Values
Useworld:set() to set a component’s value for an entity:
world:set() function:
- Adds the component if the entity doesn’t have it
- Updates the component if the entity already has it
- Enforces type safety (the value must match the component’s type)
world:set() acts as both an add and update operation. You don’t need separate functions for adding vs. updating components.Getting Component Values
Useworld:get() to retrieve a component’s value from an entity:
T? (T or nil), not just T. The entity might not have the component, or the entity might have been deleted. Always handle the nil case:
Removing Components
Useworld:remove() to remove a component from an entity:
world:remove() function:
- Removes the component and its data from the entity
- Is idempotent (safe to call multiple times)
- Does not delete the entity itself (use
world:delete()for that)
Mental Model
Components are keys to storage columns. When you useset(), get(), or remove(), you’re using the component ID to index into the ECS storage:
In reality, the storage uses archetypes for efficiency, but this mental model is useful for understanding how the APIs work. See Archetypes for more details.