Skip to main content

Query

A query made by [World:query]: the entities that have all of its ids, with their values.

for entity, position, velocity in world:query(Position, Velocity):without(Frozen) do
end
world:query(Position, Velocity):each(function(entity, position, velocity) end)
  • A query never goes stale: its ids may be created and deleted at any time.
  • A query written inline in a system is cheap: queries of the same shape share one state, so an inline query allocates only a small handle, and an iteration of a stored query allocates nothing.
  • The current entity may be changed or deleted inside the loop. Changes of other entities may or may not be seen by the running loop; an entity deleted ahead of the loop may still be returned once (check World:contains).
  • Loops over the same query may be nested. The order is ascending by entity slot.

Functions​

with​

Query:with(...: Id) → Query

Required ids whose values are not returned. Each call replaces the earlier ids of with.

without​

Query:without(...: Id) → Query

Excluded ids. Each call replaces the earlier ids of without.

any​

Query:any(...: Id) → Query

An OR term: the entity has at least one of the ids. Several calls add several terms.

cached​

Query:cached() → Query

Keeps what the query observed and its iteration state between loops. Shared queries (without a concrete pair and without change filters) are cached already; call it on a query with a concrete pair or change filters that is stored and iterated many times.

added​

Query:added(
id: Id--

a tracked id (see World:track)

) → Query

Change filter: the entities that got id added since the previous iteration of this query. Each query keeps the first tick it has not seen, up to the last 8 ticks.

changed​

Query:changed(
id: Id--

a tracked id (see World:track)

) → Query

Change filter: the entities whose value of id was set again since the previous iteration of this query.

removed​

Query:removed(
id: Id--

a tracked id (see World:track)

) → Query

Change filter: the entities that lost id since the previous iteration of this query and are still alive.

each​

Query:each(callback: (
entity: Entity,
...any
) → ()) → ()

Calls callback(entity, ...values) for every match: the fastest loop.

has​

Query:has(entity: Id) → boolean

Whether the entity matches the query.

spans​

Query:spans() → () → (
number?,
number?,
number
)--

an iterator of first, last, mask

The fast path: runs of matching slots. When mask == 0xFFFFFFFF every slot of first..last matches; otherwise first..last is one 32-slot word and the set bits of mask are the matching slots (first + bit index). Read the values with World:column and the entities with World:alive_table.

count​

Query:count() → number

The number of matches. It does not consume change filters.

add_all​

Query:add_all(id: Id) → ()

Adds id to every entity that matches now. A component or tag without hooks is added 32 entities at a time; pairs and ids with hooks go entity by entity, and every hook runs.

set_all​

Query:set_all(
id: Id<V>,
value: V--

the same value for every match

) → ()

Sets id to value on every entity that matches now.

remove_all​

Query:remove_all(id: Id) → ()

Removes id from every entity that matches now.

delete_all​

Query:delete_all() → ()

Deletes every entity that matches now.

Show raw api
{
    "functions": [
        {
            "name": "with",
            "desc": "Required ids whose values are not returned. Each call replaces the earlier ids of `with`.",
            "params": [
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "Id"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Query"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 905,
                "path": "src/init.luau"
            }
        },
        {
            "name": "without",
            "desc": "Excluded ids. Each call replaces the earlier ids of `without`.",
            "params": [
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "Id"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Query"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 913,
                "path": "src/init.luau"
            }
        },
        {
            "name": "any",
            "desc": "An OR term: the entity has at least one of the ids. Several calls add several terms.",
            "params": [
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "Id"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Query"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 921,
                "path": "src/init.luau"
            }
        },
        {
            "name": "cached",
            "desc": "Keeps what the query observed and its iteration state between loops. Shared queries\n(without a concrete pair and without change filters) are cached already; call it on a\nquery with a concrete pair or change filters that is stored and iterated many times.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Query"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 930,
                "path": "src/init.luau"
            }
        },
        {
            "name": "added",
            "desc": "Change filter: the entities that got `id` added since the previous iteration of this\nquery. Each query keeps the first tick it has not seen, up to the last 8 ticks.",
            "params": [
                {
                    "name": "id",
                    "desc": "a tracked id (see [World:track])",
                    "lua_type": "Id"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Query"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 939,
                "path": "src/init.luau"
            }
        },
        {
            "name": "changed",
            "desc": "Change filter: the entities whose value of `id` was set again since the previous\niteration of this query.",
            "params": [
                {
                    "name": "id",
                    "desc": "a tracked id (see [World:track])",
                    "lua_type": "Id"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Query"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 948,
                "path": "src/init.luau"
            }
        },
        {
            "name": "removed",
            "desc": "Change filter: the entities that lost `id` since the previous iteration of this query\nand are still alive.",
            "params": [
                {
                    "name": "id",
                    "desc": "a tracked id (see [World:track])",
                    "lua_type": "Id"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Query"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 957,
                "path": "src/init.luau"
            }
        },
        {
            "name": "each",
            "desc": "Calls `callback(entity, ...values)` for every match: the fastest loop.",
            "params": [
                {
                    "name": "callback",
                    "desc": "",
                    "lua_type": "(entity: Entity, ...any) -> ()"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 964,
                "path": "src/init.luau"
            }
        },
        {
            "name": "has",
            "desc": "Whether the entity matches the query.",
            "params": [
                {
                    "name": "entity",
                    "desc": "",
                    "lua_type": "Id"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 972,
                "path": "src/init.luau"
            }
        },
        {
            "name": "spans",
            "desc": "The fast path: runs of matching slots. When `mask == 0xFFFFFFFF` every slot of\n`first..last` matches; otherwise `first..last` is one 32-slot word and the set bits of\n`mask` are the matching slots (`first + bit index`). Read the values with\n[World:column] and the entities with [World:alive_table].",
            "params": [],
            "returns": [
                {
                    "desc": "an iterator of first, last, mask",
                    "lua_type": "() -> (number?, number?, number)"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 982,
                "path": "src/init.luau"
            }
        },
        {
            "name": "count",
            "desc": "The number of matches. It does not consume change filters.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "number"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 989,
                "path": "src/init.luau"
            }
        },
        {
            "name": "add_all",
            "desc": "Adds `id` to every entity that matches now. A component or tag without hooks is added 32\nentities at a time; pairs and ids with hooks go entity by entity, and every hook runs.",
            "params": [
                {
                    "name": "id",
                    "desc": "",
                    "lua_type": "Id"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 997,
                "path": "src/init.luau"
            }
        },
        {
            "name": "set_all",
            "desc": "Sets `id` to `value` on every entity that matches now.",
            "params": [
                {
                    "name": "id",
                    "desc": "",
                    "lua_type": "Id<V>"
                },
                {
                    "name": "value",
                    "desc": "the same value for every match",
                    "lua_type": "V"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 1005,
                "path": "src/init.luau"
            }
        },
        {
            "name": "remove_all",
            "desc": "Removes `id` from every entity that matches now.",
            "params": [
                {
                    "name": "id",
                    "desc": "",
                    "lua_type": "Id"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 1012,
                "path": "src/init.luau"
            }
        },
        {
            "name": "delete_all",
            "desc": "Deletes every entity that matches now.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 1018,
                "path": "src/init.luau"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "Query",
    "desc": "A query made by [World:query]: the entities that have all of its ids, with their values.\n\n```lua\nfor entity, position, velocity in world:query(Position, Velocity):without(Frozen) do\nend\nworld:query(Position, Velocity):each(function(entity, position, velocity) end)\n```\n\n- A query never goes stale: its ids may be created and deleted at any time.\n- A query written inline in a system is cheap: queries of the same shape share one\n  state, so an inline query allocates only a small handle, and an iteration of a stored\n  query allocates nothing.\n- The current entity may be changed or deleted inside the loop. Changes of other\n  entities may or may not be seen by the running loop; an entity deleted ahead of the\n  loop may still be returned once (check [World:contains]).\n- Loops over the same query may be nested. The order is ascending by entity slot.",
    "source": {
        "line": 897,
        "path": "src/init.luau"
    }
}