2024-07-26 02:36:30 +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.
## Functions
2024-07-30 13:41:28 +00:00
### with()
2024-07-26 02:36:30 +00:00
```luau
2024-07-30 13:41:28 +00:00
function query:with(
...: Entity -- The IDs to query with
): Query
2024-07-26 02:36:30 +00:00
```
2024-07-30 13:41:28 +00:00
Adds 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-07-26 02:36:30 +00:00
Example:
::: code-group
```luau [luau]
2024-07-30 13:41:28 +00:00
for id, position in world:query(Position):with(Velocity) do
-- Do something
end
2024-07-26 02:36:30 +00:00
```
```ts [typescript]
2024-07-30 13:41:28 +00:00
for (const [id, position] of world.query(Position).with(Velocity)) {
// Do something
}
2024-07-26 02:36:30 +00:00
```
:::
2024-07-30 13:41:28 +00:00
:::info
Put the IDs inside of `world:query()` instead if you need the data.
:::
### without()
2024-07-26 02:36:30 +00:00
```luau
2024-07-30 13:41:28 +00:00
function query:without(
...: Entity -- The IDs to filter against.
): Query -- Returns the Query
2024-07-26 02:36:30 +00:00
```
2024-07-30 13:41:28 +00:00
Removes entities with the provided IDs from the query.
2024-07-26 02:36:30 +00:00
Example:
::: code-group
```luau [luau]
2024-07-30 13:41:28 +00:00
for _ in world:query(Position):without(Velocity) do
-- Do something
end
2024-07-26 02:36:30 +00:00
```
```ts [typescript]
2024-07-30 13:41:28 +00:00
for (const _ of world.query(Position).without(Velocity)) {
// Do something
}
2024-07-26 02:36:30 +00:00
```
2024-07-30 13:41:28 +00:00
:::
### replace()
2024-07-26 02:36:30 +00:00
```luau
2024-07-30 13:41:28 +00:00
function query:replace(
fn: (entity: Entity, ...: T...) -> U... -- ): () -- The callback that will transform the entities' data
2024-07-26 02:36:30 +00:00
```
2024-07-30 13:41:28 +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-07-26 02:36:30 +00:00
Example:
::: code-group
```luau [luau]
2024-07-30 13:41:28 +00:00
world:query(Position, Velocity):replace(function(e, position, velocity)
return position + velocity, velocity * 0.9
end
2024-07-26 02:36:30 +00:00
```
```ts [typescript]
2024-07-30 13:41:28 +00:00
world
.query(Position, Velocity)
.replace((e, position, velocity) =>
$tuple(position.add(velocity), velocity.mul(0.9)),
);
2024-07-26 02:36:30 +00:00
```
:::
2024-07-30 13:41:28 +00:00
### archetypes()
2024-07-26 02:36:30 +00:00
```luau
2024-07-30 13:41:28 +00:00
function query:archetypes(): { Archetype }
2024-07-26 02:36:30 +00:00
```
2024-07-30 13:41:28 +00:00
Returns the matching archetypes of the query.
2024-07-26 02:36:30 +00:00
Example:
::: code-group
```luau [luau]
2024-07-30 13:41:28 +00:00
for i, archetype in world:query(Position, Velocity):archetypes() do
local field = archetype.records
local columns = archetype.columns
for row, entity in archetype.entities do
local position = columns[field[Position]][row]
local velocity = columns[field[Velocity]][row]
-- Do something
end
2024-07-26 02:36:30 +00:00
end
```
2024-07-30 13:41:28 +00:00
:::
2024-07-26 02:36:30 +00:00
2024-07-30 13:41:28 +00:00
:::info
This function is meant for internal usage. Use this if you want to maximize performance by inlining the iterator.
2024-07-26 02:36:30 +00:00
:::