Skip to content

Queries

Queries provide the ability to fetch data about the entities that are stored in the fiasco game engine. Similar to a database select statement.

Queries can only be performed in systems by specifying typed arguments to Query<T> that will be dependency injected.

ts
import type { Component, Query } from '@vaguevoid/fiasco'

type Animal = Component<{...}>

function mySystem(animals: Query<[Animal]>) {
  for (const [animal] of animals) {
    // update any field here every frame
    animal.speed++
  }
}

Queries will return results for entities that have the same components attached as specified in the query. To get the entity ID at the same time, include it in the Query:

ts
import type { Component, Query, EntityId } from '@vaguevoid/fiasco'

type Animal = Component<{...}>

function mySystem(animals: Query<[Animal, EntityId]>) {
  for (const [animal, entity] of animals) {
    // update any field here every frame
    animal.speed++
    if (animal.speed > 7000) {
      engine.despawn(entity.id)
    }
  }
}

WARNING

Entities are stored in binary data buffers, thus they do not behave exactly like normal JavaScript variables. Do not try to store data retrieved from queries to outer-scopes and access outside of the system because they are not normal objects.

ts
// BAD
let data = []
function sys(animals: Query<[Animal]>) {
  animals.forEach(([animal]) => data.push(animal)) // bad
}