Arch.System.SourceGenerator
Arch.System.SourceGenerator, automatically generates queries for you.
Last updated
Was this helpful?
Arch.System.SourceGenerator, automatically generates queries for you.
Last updated
Was this helpful?
The Arch.System.SourceGenerator
package provides some code generation utils. With them, queries can be written virtually by themselves which saves some boilerplate code.
Query methods can be generated in as long as they are partial. However it makes most sense in the . The attributes can be used to meaningfully describe what query to generate, and the query will always call the annotated method. And the best, they can even be called manually!
Let's take a look at the whole thing and what is possible with it.
Each method marked with [Query]
is extended to a Query
by the source generation and called once for each Entity
that matches it. The method signature and additional annotations act as a filter. This means that the MoveEntity
method from above is called for each Entity
with Position
and Velocity
. We only write the method with which we process each Entity
and what we need... and Arch.System.SourceGenerator
takes care of the rest.
A method with the same name and query is generated for each of these selected methods. This query method then calls our selected method for each Entity
. E.g. MoveEntity
which is getting called for each fitting Entity
and MoveEntityQuery
, the generated method that runs the query and calls MoveEntity
for each Entity
. This happens for every method within a class that has been marked with Query
. So also for StopDeadEntities
.
In addition, if BaseSystem.Update
has not been implemented itself, a BaseSystem.Update
is generated which calls all Queries
according to their sequence in the system. So all you have to do is call BaseSystem.Update()
or the generated query directly.
It can be that easy, I bet that blew your mind, didn't it?
If you use the generator as above then an Update
method is automatically generated for you which calls all queries one after the other. However, if you want to have control over the update method yourself, you can do it this way:
However, you do not necessarily have to use the BaseSystem
with the SourceGenerator. Instead, you can have queries generated wherever you want!
Arch supports parallel queries directly out of the box. The source generator also does, with a simple attribute you can ensure that the query is executed on multiple threads: