2025-02-19 16:10:38 +00:00
|
|
|
# Query API
|
2025-01-15 18:59:07 +00:00
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
Queries allow you to efficiently find and iterate over entities that have a specific set of components.
|
2025-01-15 18:59:07 +00:00
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
## Methods
|
2025-01-15 18:59:07 +00:00
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
### iter()
|
2025-01-15 18:59:07 +00:00
|
|
|
```luau
|
|
|
|
function Query:iter(): () -> (Entity, ...)
|
|
|
|
```
|
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
Returns an iterator that yields matching entities and their component values.
|
2025-01-15 18:59:07 +00:00
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
Example:
|
|
|
|
::: code-group
|
|
|
|
```luau [luau]
|
|
|
|
for id, position, velocity in world:query(Position, Velocity):iter() do
|
|
|
|
-- Do something with position and velocity
|
|
|
|
end
|
|
|
|
```
|
|
|
|
```typescript [typescript]
|
|
|
|
for (const [id, position, velocity] of world.query(Position, Velocity)) {
|
|
|
|
// Do something with position and velocity
|
|
|
|
}
|
|
|
|
```
|
|
|
|
:::
|
2025-01-15 18:59:07 +00:00
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
### with()
|
2025-01-15 18:59:07 +00:00
|
|
|
```luau
|
2025-02-19 16:10:38 +00:00
|
|
|
function Query:with(...: Entity): Query
|
2025-01-15 18:59:07 +00:00
|
|
|
```
|
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
Adds components to filter by, but doesn't return their values in iteration. Useful for filtering by tags.
|
|
|
|
|
2025-01-15 18:59:07 +00:00
|
|
|
Example:
|
|
|
|
::: code-group
|
|
|
|
```luau [luau]
|
2025-02-19 16:10:38 +00:00
|
|
|
-- Only get position for entities that also have Velocity
|
2025-01-15 18:59:07 +00:00
|
|
|
for id, position in world:query(Position):with(Velocity) do
|
2025-02-19 16:10:38 +00:00
|
|
|
-- Do something with position
|
2025-01-15 18:59:07 +00:00
|
|
|
end
|
|
|
|
```
|
2025-02-19 16:10:38 +00:00
|
|
|
```typescript [typescript]
|
2025-01-15 18:59:07 +00:00
|
|
|
for (const [id, position] of world.query(Position).with(Velocity)) {
|
2025-02-19 16:10:38 +00:00
|
|
|
// Do something with position
|
2025-01-15 18:59:07 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
:::
|
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
### without()
|
2025-01-15 18:59:07 +00:00
|
|
|
```luau
|
2025-02-19 16:10:38 +00:00
|
|
|
function Query:without(...: Entity): Query
|
2025-01-15 18:59:07 +00:00
|
|
|
```
|
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
Excludes entities that have any of the specified components.
|
2025-01-15 18:59:07 +00:00
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
Example:
|
2025-01-15 18:59:07 +00:00
|
|
|
::: code-group
|
|
|
|
```luau [luau]
|
2025-02-19 16:10:38 +00:00
|
|
|
-- Get position for entities that don't have Velocity
|
|
|
|
for id, position in world:query(Position):without(Velocity) do
|
|
|
|
-- Do something with position
|
2025-01-15 18:59:07 +00:00
|
|
|
end
|
|
|
|
```
|
2025-02-19 16:10:38 +00:00
|
|
|
```typescript [typescript]
|
|
|
|
for (const [id, position] of world.query(Position).without(Velocity)) {
|
|
|
|
// Do something with position
|
2025-01-15 18:59:07 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
:::
|
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
### cached()
|
|
|
|
```luau
|
|
|
|
function Query:cached(): Query
|
|
|
|
```
|
2025-01-15 18:59:07 +00:00
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
Returns a cached version of the query for better performance when using the same query multiple times.
|
2025-01-15 18:59:07 +00:00
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
### archetypes()
|
2025-01-15 18:59:07 +00:00
|
|
|
```luau
|
|
|
|
function Query:archetypes(): { Archetype }
|
|
|
|
```
|
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
Returns the matching archetypes for low-level query customization.
|
2025-01-15 18:59:07 +00:00
|
|
|
|
2025-02-19 16:10:38 +00:00
|
|
|
:::info
|
|
|
|
This method is for advanced users who need fine-grained control over query behavior at the archetype level.
|
|
|
|
:::
|
|
|
|
|
|
|
|
Example:
|
|
|
|
```luau
|
2025-01-15 18:59:07 +00:00
|
|
|
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
|
|
|
|
```
|