Working With Systems

TinyEcs

Working With Systems

[This is preliminary documentation and is subject to change.]

A brief introduction to working with ComponentSystemT

Creating a system that operates on multiple components
C#
public class MovementSystem : ComponentSystem<UpdateMessage>
{
  public class Data
  {
    public int Length;
    public RwDataStream<Position> Positions;
    public RoDataStream<Heading> Headings;
  }
  [Group] public Data data; // Will select all entities with a Position and Heading component
  public void Execute(World world, UpdateMessage message)
  {
    for (var i = 0; i < data.Length; i++)
    {
      // The component streams are always stored in parallel
      data.Positions[i].Vector += data.Headings[i].Vector * message.DeltaTime;
    }
  }
}
Getting a stream of entities
C#
public class HealthSystem : ComponentSystem<UpdateMessage>
{
  private SomeDependency dependency;

  public class Data
  {
    public int Length;
    public RoDataStream<Entity> Entities; // Must be this exact type, name does not matter.
    public RoDataStream<Health> Healths;
  }
  [Group] public Data data;

  public HealthSystem(SomeDependency dependency)
  {
    this.dependency = dependency;
  }

  public void Execute(World world, UpdateMessage message)
  {
     for (var i = 0; i < data.Length; i++)
     {
      if (data.Healths[i].Value <= 0)
      {
        dependency.DoSomethingWithEntity(data.Entities[i]);
      }
    }
  }
}
Including entities with a tag
C#
public class RegenSystem : ComponentSystem<UpdateMessage>
{
  public class Data
  {
    public int Length;
    public RoDataStream<Health> Healths;
    public RegenTag RegenTag; // Entities must have the RegenTag to be selected
  }
  [Group] public Data data;

  public void Execute(World world, UpdateMessage message)
  {
     for (var i = 0; i < data.Length; i++)
     {
      data.Healths[i].Value = Math.Min(data.Healths[i].MaxValue, data.Healths[i].Value + message.deltaTime);
    }
  }
}
Excluding entities marked by a tag
C#
public class MovementSystem : ComponentSystem<UpdateMessage>
{
  public class Data
  {
    public int Length;
    public RwDataStream<Position> Positions;
    public RoDataStream<Heading> Headings;
    [Exclude] public FrozenTag ExcludeFrozenTag; // Exclude the FrozenTag
    // As many tags as desired can be added
  }
  [Group] public Data data; // Will select all entities with a Position and Heading component and without a FrozenTag tag.
  public void Execute(World world, UpdateMessage message)
  {
    for (var i = 0; i < data.Length; i++)
    {
      data.Positions[i].Vector += data.Headings[i].Vector * message.DeltaTime;
    }
  }
}