Use this file to discover all available pages before exploring further.
Queries allow you to search and iterate over entities that match specific component criteria. jecs provides two query types: Query for one-time use and CachedQuery for repeated iterations.
Modifies the query to include entities that have additional components. The components are not returned in the iterator, but entities must have them to match.
-- Query entities with Position and Velocity that also have Healthfor entity, pos, vel in world:query(Position, Velocity):with(Health) do -- Only entities with all three componentsend
// Query entities with Position and Velocity that also have Healthfor (const [entity, pos, vel] of world.query(Position, Velocity).with(Health)) { // Only entities with all three components}
-- Query entities with Position but without Velocityfor entity, position in world:query(Position):without(Velocity) do -- Static entities onlyend
// Query entities with Position but without Velocity for (const [entity, position] of world.query(Position).without(Velocity)) { // Static entities only}
Returns all archetypes that match the query criteria.Returns:Archetype<T>[]
local archetypes = world:query(Position):archetypes()for _, archetype in archetypes do print("Archetype ID:", archetype.id) print("Entity count:", #archetype.entities)end
Converts a query to a cached query for efficient repeated iterations. Cache the query AFTER applying all filters.Returns:CachedQuery<T>
local cachedQuery = world:query(Position, Velocity):with(Health):cached()-- Use the cached query multiple times efficientlyfor entity, pos, vel in cachedQuery do -- Iteration 1endfor entity, pos, vel in cachedQuery do -- Iteration 2 - uses cached archetypesend
const cachedQuery = world.query(Position, Velocity).with(Health).cached()// Use the cached query multiple times efficientlyfor (const [entity, pos, vel] of cachedQuery) { // Iteration 1}for (const [entity, pos, vel] of cachedQuery) { // Iteration 2 - uses cached archetypes}
A cached query stores the list of matching archetypes and automatically updates when archetypes are created or destroyed. This makes repeated iterations much faster.
-- Iterate over all entities with Position and Velocityfor entity, position, velocity in world:query(Position, Velocity) do position.x = position.x + velocity.x position.y = position.y + velocity.yend
-- Find entities with Position and Health but not Deadfor entity, pos, health in world:query(Position, Health):without(Dead) do if health.value > 0 then -- Process living entities endend
-- Entities with Position, Velocity, and Health, but not Dead or Frozenlocal query = world:query(Position, Velocity, Health) :without(Dead, Frozen) :cached()for entity, pos, vel, health in query do -- Process active entitiesend