Use this file to discover all available pages before exploring further.
Pairs are the foundation of relationships in jecs. They encode two entity IDs into a single 53-bit ID, allowing the ECS to treat pairs the same way as regular component IDs at the storage level.
First element: The relationship/predicate (e.g., “Likes”, “Owns”)
Second element: The target/object (e.g., “alice”, “car”)
Pairs use an offset (ECS_PAIR_OFFSET = 2^48) in the 53-bit ID space. This makes pairs larger than any regular entity ID, allowing quick differentiation.
local Eats = world:component()local Likes = world:entity()local Apples = world:entity()local alice = world:entity()local bob = world:entity()world:add(bob, pair(Eats, Apples))world:add(bob, pair(Likes, alice))-- Check if bob has any Eats relationshipprint(world:has(bob, pair(Eats, jecs.Wildcard))) -- true-- Find all entities that eat somethingfor entity in world:query(pair(Eats, jecs.Wildcard)) do local food = world:target(entity, Eats) print(`Entity {entity} eats {food}`)end-- Find all entities that like someonefor entity in world:query(pair(Likes, jecs.Wildcard)) do local target = world:target(entity, Likes) print(`Entity {entity} likes {target}`)end
local Eats = world:entity()local Apples = world:entity()local bob = world:entity()world:add(bob, pair(Eats, Apples))-- Get the first (and only) targetlocal first = world:target(bob, Eats, 0)print(first == Apples) -- true-- Omitting index defaults to 0local default = world:target(bob, Eats)print(default == Apples) -- true
An entity can have the same relationship with multiple targets:
local Likes = world:entity()local alice = world:entity()local charlie = world:entity()local bob = world:entity()world:add(bob, pair(Likes, alice))world:add(bob, pair(Likes, charlie))-- Iterate through all targets (0-based indexing)for entity in world:query(pair(Likes, jecs.Wildcard)) do local nth = 0 local target = world:target(entity, Likes, nth) while target do print(`Entity {entity} likes {target}`) nth += 1 target = world:target(entity, Likes, nth) endend
Target indices are 0-based. The first target is at index 0, the second at index 1, and so on.
Find all relationships that point to a specific entity:
-- Find all entities that have ANY relationship with alicefor entity in world:query(pair(jecs.Wildcard, alice)) do print(`Entity {entity} has some relationship with alice`)end