Skip to content

Latest commit

 

History

History
71 lines (60 loc) · 1.81 KB

queries.md

File metadata and controls

71 lines (60 loc) · 1.81 KB

What is a query?

Queries are used to filter entities based on their current archetype.
A query automatically update entity archetypes on each calls.

How to create a query (Raw)

EntityQuery characterQuery = new EntityQuery(
    world,
    // The entities must have components that contains 'all' components.
    all: new ComponentType[] { characterComponent, positionComponent },
    // The entities must NOT have components contained in 'none' filter
    none: new ComponentType[] { enemyComponent },
    // The entities must atleast have one component contained in 'or' filter
    or: new ComponentType[] { }
);

Usage

var positionAccessor = world.GetSparseAccessor(positionComponent);
foreach (var UEntityHandle entity in characterQuery)
{
    ref var position = ref positionAccessor[entity];
}

How to create a query (Source Generator)

partial struct MyQuery : IQuery<
    Read<Position>,
    With<Character>
    None<Enemey>
> {}

Usage

var query = new MyQuery(world);
foreach (var (entity, position) in query)
{
    var y = position.Y;

    // RPosition type -> original Position type
    ref var pos = ref position.__ref;
}

Available filters

  • Write: Push T in the 'all' filter, and indicate that we will write and read to it
  • Read: Push T in the 'all' filter, and indicate that we will read to it
  • With: Push T in the 'all' filter, but don't read or write to it
  • None: Push T in the 'none' filter
  • Or: Push T in the 'or' filter

Singleton

  • ISingleton: Transform queries function for singleton usage (see bottom)

Usage

partial struct GameTime : ISparseComponent
{
    public float Time, Delta;
}

partial struct TimeQuery : IQuery<Read<GameTime>>, ISingleton {}

var query = new TimeQuery(world);

var time = query.Time;
var delta = query.Delta;