2024-09-07 20:12:07 +00:00
# Query
A World contains entities which have components. The World is queryable and can be used to get entities with a specific set of components.
2024-09-29 07:13:21 +00:00
# Methods
2024-09-07 20:12:07 +00:00
2024-09-29 07:13:21 +00:00
## drain
2024-09-20 23:28:29 +00:00
This method will impede it from being reset when the query is being iterated.
2024-09-07 20:12:07 +00:00
```luau
function query:drain(): Query
```
2024-09-29 07:13:21 +00:00
## next
2024-09-20 23:28:29 +00:00
Get the next result in the query. Drain must have been called beforehand or otherwise it will error.
2024-09-07 20:12:07 +00:00
```luau
function query:next(): Query
```
2024-09-29 07:13:21 +00:00
## with
2024-09-20 23:28:29 +00:00
Adds components (IDs) to query with, but will not use their data. This is useful for Tags or generally just data you do not care for.
2024-09-07 20:12:07 +00:00
```luau
function query:with(
...: Entity -- The IDs to query with
): Query
```
Example:
::: code-group
```luau [luau]
for id, position in world:query(Position):with(Velocity) do
-- Do something
end
```
```ts [typescript]
for (const [id, position] of world.query(Position).with(Velocity)) {
// Do something
}
```
:::
:::info
Put the IDs inside of `world:query()` instead if you need the data.
:::
2024-09-29 07:13:21 +00:00
## without
2024-09-20 23:28:29 +00:00
Removes entities with the provided components from the query.
2024-09-07 20:12:07 +00:00
```luau
function query:without(
...: Entity -- The IDs to filter against.
): Query -- Returns the Query
```
Example:
2024-09-20 23:28:29 +00:00
::: code-group
2024-09-07 20:12:07 +00:00
```luau [luau]
2024-09-20 23:28:29 +00:00
for entity, position in world:query(Position):without(Velocity) do
2024-09-07 20:12:07 +00:00
-- Do something
end
```
```ts [typescript]
2024-09-20 23:28:29 +00:00
for (const [entity, position] of world.query(Position).without(Velocity)) {
2024-09-07 20:12:07 +00:00
// Do something
}
```
:::
2024-09-29 07:13:21 +00:00
## replace
2024-09-20 23:28:29 +00:00
This function takes a callback which is given the current queried data of each matching entity. The values returned by the callback will be set as the new data for each given ID on the entity.
2024-09-07 20:12:07 +00:00
```luau
function query:replace(
fn: (entity: Entity, ...: T...) -> U... -- ): () -- The callback that will transform the entities' data
```
Example:
2024-09-20 23:28:29 +00:00
::: code-group
2024-09-07 20:12:07 +00:00
```luau [luau]
world:query(Position, Velocity):replace(function(e, position, velocity)
return position + velocity, velocity * 0.9
end
```
```ts [typescript]
world
.query(Position, Velocity)
.replace((e, position, velocity) =>
$tuple(position.add(velocity), velocity.mul(0.9)),
);
```
:::
2024-09-29 07:13:21 +00:00
## archetypes
2024-09-20 23:28:29 +00:00
Returns the matching archetypes of the query.
2024-09-07 20:12:07 +00:00
```luau
function query.archetypes(): { Archetype }
```
Example:
```luau [luau]
for i, archetype in world:query(Position, Velocity).archetypes() do
local columns = archetype.columns
local field = archetype.records
local P = field[Position]
local V = field[Velocity]
for row, entity in archetype.entities do
local position = columns[P][row]
local velocity = columns[V][row]
-- Do something
end
end
```
:::info
This function is meant for internal usage. Use this if you want to maximize performance by inlining the iterator.
:::