Skip to content

Commit

Permalink
Tests: add CustomSystem example
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Dec 28, 2024
1 parent e1b0fa4 commit 5ca5f9c
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Tests/ECS/Examples/HelloSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,42 @@ protected override void OnUpdate() {
}
}

[Test]
public static void CustomSystem()
{
var world = new EntityStore();
var entity = world.CreateEntity(new Position(0, 0, 0));
var root = new SystemRoot(world) {
new CustomQuerySystem()
};
root.Update(default);

Console.WriteLine($"entity: {entity}"); // entity: id: 1 [Position, Velocity]
}

/// The example shows how to create a custom system that<br/>
/// - creates a <see cref="customQuery"/> and <br/>
/// - make structural changes via the parent group <see cref="QuerySystemBase.CommandBuffer"/>.<br/>
/// <br/>
/// The system adds a Velocity component for every entity having a Position component.
class CustomQuerySystem : QuerySystem
{
private ArchetypeQuery<Position> customQuery;

protected override void OnAddStore(EntityStore store) {
customQuery = store.Query<Position>();
base.OnAddStore(store);
}

/// Executes the <see cref="customQuery"/> instead of the base class <see cref="QuerySystem.Query"/>.
protected override void OnUpdate() {
var buffer = CommandBuffer;
customQuery.ForEachEntity((ref Position component1, Entity entity) => {
buffer.AddComponent(entity.Id, new Velocity());
});
}
}

}

}

0 comments on commit 5ca5f9c

Please sign in to comment.