Skip to content
Martin Evans edited this page Jul 29, 2024 · 2 revisions

A query executes some code for all entities which match the component type filters specified by a QueryDescription. The code to execute for each entity is defined with a type implementing IQueryN:

public struct ExampleQueryType
    : IQuery2<Component1, Component2>
{
    public void Execute(Entity e, ref Component1 c1, ref Component2 c2) { /* whateever */ }
}

void DoQuery()
{
    world.Execute<ExampleQueryType, Component1, Component2>();
}

There are a number of overloads possible:

  • Optionally, supply a QueryDescription. If not supplied the type arguments are used.
  • Optionally, pass a query instance. If not supplied one will be constructed with new().
    • If a query instance is passed, it may be passed by ref (this allows a mutable struct to be used).

The QueryDescription may be passed by ref. If a null query is passed this way a default one will be constructed using the type parameters (as usual) and it will be written into the ref parameter. This acts as a cache - if the same field is passed by ref next time the query is executed the same query can be used.

Parallelism

There is a parallel mode of queries available, which will execute all entities in parallel. Entities are split among threads in batches, the batch size can be set with the batchSize parameter.

void DoParallelQuery()
{
    world.ExecuteParallel<ExampleQueryType, Component1, Component2>(batchSize: 128);
}
Clone this wiki locally