jecs/docs/api/query.md

89 lines
1.8 KiB
Markdown
Raw Normal View History

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.
# Methods
2024-09-07 20:12:07 +00:00
## with
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.
:::
## without
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:
::: code-group
2024-09-07 20:12:07 +00:00
```luau [luau]
for entity, position in world:query(Position):without(Velocity) do
2024-09-07 20:12:07 +00:00
-- Do something
end
```
```ts [typescript]
for (const [entity, position] of world.query(Position).without(Velocity)) {
2024-09-07 20:12:07 +00:00
// Do something
}
```
:::
## archetypes
Returns the matching archetypes of the query.
2024-09-07 20:12:07 +00:00
```luau
2024-10-12 20:00:51 +00:00
function query:archetypes(): { Archetype }
2024-09-07 20:12:07 +00:00
```
Example:
```luau [luau]
2024-10-12 20:00:51 +00:00
for i, archetype in world:query(Position, Velocity):archetypes() do
2024-09-07 20:12:07 +00:00
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 people who want to really customize their query behaviour at the archetype-level
2024-09-07 20:12:07 +00:00
:::