Improvements to "Query" API Docs (#130)

* Improvements to "Query" API Docs

Minor improvements, but mostly made this fit more with the PR i made for World

* Update query.md

* Update query.md
This commit is contained in:
silly-spongus 2024-10-06 01:39:47 +00:00 committed by GitHub
parent c80bb0e2fd
commit 9a0a578acd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,27 +2,28 @@
A World contains entities which have components. The World is queryable and can be used to get entities with a specific set of components. 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 # Methods
## drain() ## drain
This method will impede it from being reset when the query is being iterated.
```luau ```luau
function query:drain(): Query function query:drain(): Query
``` ```
This function will impede it from being reset when the query is being iterated.
## next() ## next
Get the next result in the query. Drain must have been called beforehand or otherwise it will error.
```luau ```luau
function query:next(): Query function query:next(): Query
``` ```
Get the next result in the query. Drain must have been called beforehand or otherwise it will error.
## with() ## 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.
```luau ```luau
function query:with( function query:with(
...: Entity -- The IDs to query with ...: Entity -- The IDs to query with
): Query ): Query
``` ```
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.
Example: Example:
::: code-group ::: code-group
@ -45,43 +46,41 @@ for (const [id, position] of world.query(Position).with(Velocity)) {
Put the IDs inside of `world:query()` instead if you need the data. Put the IDs inside of `world:query()` instead if you need the data.
::: :::
## without() ## without
Removes entities with the provided components from the query.
```luau ```luau
function query:without( function query:without(
...: Entity -- The IDs to filter against. ...: Entity -- The IDs to filter against.
): Query -- Returns the Query ): Query -- Returns the Query
``` ```
Removes entities with the provided IDs from the query.
Example: Example:
::: code-group
::: code-group
```luau [luau] ```luau [luau]
for _ in world:query(Position):without(Velocity) do for entity, position in world:query(Position):without(Velocity) do
-- Do something -- Do something
end end
``` ```
```ts [typescript] ```ts [typescript]
for (const _ of world.query(Position).without(Velocity)) { for (const [entity, position] of world.query(Position).without(Velocity)) {
// Do something // Do something
} }
``` ```
::: :::
## replace() ## replace
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.
```luau ```luau
function query:replace( function query:replace(
fn: (entity: Entity, ...: T...) -> U... -- ): () -- The callback that will transform the entities' data fn: (entity: Entity, ...: T...) -> U... -- ): () -- The callback that will transform the entities' data
``` ```
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.
Example: Example:
::: code-group
::: code-group
```luau [luau] ```luau [luau]
world:query(Position, Velocity):replace(function(e, position, velocity) world:query(Position, Velocity):replace(function(e, position, velocity)
return position + velocity, velocity * 0.9 return position + velocity, velocity * 0.9
@ -95,18 +94,15 @@ world
$tuple(position.add(velocity), velocity.mul(0.9)), $tuple(position.add(velocity), velocity.mul(0.9)),
); );
``` ```
::: :::
## archetypes
## archetypes() Returns the matching archetypes of the query.
```luau ```luau
function query.archetypes(): { Archetype } function query.archetypes(): { Archetype }
``` ```
Returns the matching archetypes of the query.
Example: Example:
::: code-group
```luau [luau] ```luau [luau]
for i, archetype in world:query(Position, Velocity).archetypes() do for i, archetype in world:query(Position, Velocity).archetypes() do
@ -124,8 +120,6 @@ for i, archetype in world:query(Position, Velocity).archetypes() do
end end
``` ```
:::
:::info :::info
This function is meant for internal usage. Use this if you want to maximize performance by inlining the iterator. This function is meant for internal usage. Use this if you want to maximize performance by inlining the iterator.
::: :::