Arch.Extended

repository·master·Indexed 42 days ago

https://github.com/genaray/arch.extended

A suite of high-performance extensions for the Arch ECS, featuring automated systems via source generators, a high-performance event bus, entity relationships, and persistence. Includes specialized packages such as Arch.System, Arch.EventBus, and Arch.Persistence to reduce GC pressure and improve AOT compatibility.

Tokens
1.3K
Snippets
3
Records
4
Agent score
4%

What's inside Arch.Extended

  1. Overview of Arch.Extended features

    master

    Arch.Extended provides several specialized tools to enhance the core Arch ECS:

    • Systems: Organizes, reuses, and arranges queries.
    • Source Generator: Uses attributes to allow queries to write themselves via declarative syntax.
    • EventBus: A source-generated, high-performance event system.
    • LowLevel: Low-level utilities and data structures designed to reduce GC pressure.
    • Relationships: Adds simple entity relationships to Arch.
    • Persistence: Provides JSON and Binary (de)serialization for persisting Worlds.
    • AOT Source Generator: Improves AOT compatibility and reduces boilerplate code.
  2. Use Systems and Source Generators to automate queries

    master

    You can use BaseSystem<TWorld, TData> to create structured systems. By applying the [Query] attribute to methods, the Source Generator automatically creates the necessary queries and executes them during the system's update cycle. You can further refine queries using attributes like [All<T>], [Any<T>], and [None<T>].

    // Components
    public struct Position{ float X, Y };
    public struct Velocity{ float Dx, Dy };
    
    // BaseSystem provides methods for interacting with the World
    public class MovementSystem : BaseSystem<World, float>
    {
        public MovementSystem(World world) : base(world) {}
        
        // Generates a query and calls it automatically on BaseSystem.Update
        [Query]
        public void Move([Data] in float time, ref Position pos, ref Velocity vel)
        {
            pos.X += time * vel.X;
            pos.Y += time * vel.Y;
        }
        
        // Generates and filters a query and calls it automatically on BaseSystem.Update in order
        [Query]
        [All<Player, Mob>, Any<Idle, Moving>, None<Alive>] 
        public void ResetVelocity(ref Velocity vel)
        {
            vel = new Velocity{ X = 0, Y = 0 };
        }
    }
  3. Install Arch.Extended packages via dotnet CLI

    master

    Arch.Extended is a collection of extensions for the Arch ECS. You can install specific modules independently depending on your needs. Common packages include Arch.System for organizing queries, Arch.System.SourceGenerator for declarative syntax, and Arch.EventBus for high-performance event handling.

    dotnet add package Arch.System --version 1.1.0
    dotnet add package Arch.System.SourceGenerator --version 2.1.0
    dotnet add package Arch.EventBus --version 1.0.2
    dotnet add package Arch.LowLevel --version 1.1.5
    dotnet add package Arch.Relationships --version 1.0.0
    dotnet add package Arch.Persistence --version 2.0.0
    dotnet add package Arch.AOT.SourceGenerator --version 1.0.1
  4. Manage system execution with Group

    master

    The Group<TData> class allows you to group multiple systems together and control their execution lifecycle. This is useful for managing the order of execution and batching lifecycle calls like initialization and updates.

    var world = World.Create();
    
    // Create a group of systems to be controlled together
    var _systems = new Group<float>(
        "Systems",
        new MovementSystem(world),   // Run in order
        new MyOtherSystem(world),
        // ...
    );
    
    _systems.Initialize();                  // Inits all registered systems
    _systems.BeforeUpdate(in deltaTime);    // Calls .BeforeUpdate on all systems
    _systems.Update(in deltaTime);          // Calls .Update on all systems
    _systems.AfterUpdate(in deltaTime);      // Calls .AfterUpdate on all systems
    _systems.Dispose();                    // Calls .Dispose on all systems