First World

TinyEcs

First World

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

Getting started by creating your first world.

Creating a world.

  1. Create a message.

    C#
    // A message is a struct implementing the IMessage interface
    // Systems respond to specific messages
    struct UpdateMessage : IMessage
    {
      private float deltaTime;
      public UpdateMessage(float deltaTime)
      {
        this.deltaTime = deltaTime;
      }
      public float DeltaTime => deltaTime;
    }
  2. Create a component.

    C#
    // A component is a mutable struct implementing the IComponent interface
    // Components are data containers for your entities
    struct Position : IComponent
    {
      public float X, Y;
    }
  3. Create a system.

    C#
    // Create a system that executes on the UpdateMessage message
    // Systems will listen for and respond to a message of a specific type
    // Use systems to add logic for your entities and components
    class MoveRightSystem : ComponentSystem<UpdateMessage>
    {
      // A class to store the necessary component data
      public class Data
      {
        public int Length; // This will store the amount of entities in this group
                           // This field must be called Length. (case-sensitive)
        public RwDataStream<Position> Positions; // A stream of position components that allows for both reading and writing
                                                 // Only the type matters in selection, you can name it whatever you want.
      }
      [Group] public Data data; // This field will be used to inject the component data into
    
      protected override void Execute(World world, UpdateMessage message)
      {
        // Update all the positions
        for (var i = 0; i < data.Length; i++)
        {
          data.Positions[i].X += message.DeltaTime;
        }
      }
    }
  4. Create and simulate the world.

    C#
    var world = World.Create();
    var archetype = World.CreateArchetype(typeof(Position));
    var entity = World.CreateEntity(archetype);
    Console.Out.WriteLine($"x = {world.Ref<Position>(entity).X}");
    world.Post(new UpdateMessage(0.16f));
    Console.Out.WriteLine($"x = {world.Ref<Position>(entity).X}");
    world.Post(new UpdateMessage(0.16f));
    Console.Out.WriteLine($"x = {world.Ref<Position>(entity).X}");

The positions of all entities will be updated by moving to the right.

See Also