Query-Techniques

Query-Techniques, running queries more efficiently.

Arch has a number of different ways to search and iterate over entities. There are currently exactly 4 ways to issue commands to your entities in queries.

While this may seem a bit complex at first, each of these ways has advantages and disadvantages and is perfect for specific situations. Let us start...

All the queries listed below have been kept simple; each variant naturally also has the option of accessing the entity itself or other functionalities.

Query

Firstly, there is the type of query with which all examples were listed. Small and legible, you hardly have to worry.

World.Query(in desc,  => {
   Move(ref pos, ref vel);
});

Inline-Query

As an alternative, we have the slightly faster version, which is also wonderfully reusable by distributing its tasks in structs.

public struct VelocityUpdate : <Position, Velocity> {

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void Update(ref Position pos, ref Velocity vel) { 
        Move(ref pos, ref vel);
    }
}

world.<VelocityUpdate, Position, Velocity>();  

Custom enumeration

A little more manual labour, but an incredible amount of flexibility. This gives you the opportunity to design everything according to your wishes!

var query = world.Query(in desc);
foreach(ref var chunk in query.GetChunkIterator())
{
   var references =   
                          
   {
       
       ref var vel = ref Unsafe.Add(ref references.t1, entity);
       Move(ref pos, ref vel);
   }
}

Source-Generation

The last variant is the source-generated queries. These are provided by Arch's rich ecosystem and can be integrated separately. They generate the code for you as if you had written it manually. Fast, efficient and simple. What more could you want?

[Query] 
// [All<...>, Any<...>, None<...>] to filter
// [Parallel] for multithreading
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void MoveEntity(, ref Position pos,  Velocity vel)  
{
    Move(ref pos, ref vel, ref time);
}

Last updated

Was this helpful?