Skip to content
Martin Evans edited this page Sep 23, 2024 · 12 revisions

Myriad.ECS is a high performance Entity Component System (ECS) for C#.

Benchmarks

Components

Entities are just IDs, associated with a set of components. A component can be any type (managed or unmanaged) that implements IComponent.

public record struct Position(Vector2 Value) : IComponent;
public record struct Velocity(Vector2 Value) : IComponent;

IComponent is simply a tag that ensures you cannot accidentally attach something to an entity that you didn't mean to. For example adding a Vector2 directly, instead of a Position or Velocity component.

There are some special components:

Command Buffer

The only way to make structural changes to the world (creating or destroying entities, adding or removing components) is through a CommandBuffer. A CommandBuffer allows you to queue multiple commands, the world is only modified when the buffered is executed.

var buffer = new CommandBuffer(world);

// Create an entity. This returns a "buffered entity" object that can be used to resolve the real Entity when it is eventually created
var bufferedEntity = buffer.Create()
     .Set(new Position(new Vector3(1, 2, 3)))
     .Set(new Velocity(new Vector3(0, 1, 0)))
     .Set(new Mass(1));

// Execute the buffer, receive a "resolver". You can call `Resolve` on entities until this resolve is disposed.
using var resolver = buffer.Playback();

// Resolve the buffered entity into a real Entity
var entity = bufferedEntity.Resolve();

Querying

Queries can be filtered based on the components an Entity has. This is done with a QueryDescription. There are 4 types filtering a query can do:

  • Include: Entities must include this component.
  • Exclude: Entities must not include this component.
  • At Least One: Entities must contain one or more of the listed components.
  • Exactly One: Entities must contain exactly one of the listed components.

Myriad.ECS has several different querying systems:

Systems

A Myriad World can be used without any systems, as a very specialised type of in-memroy database. However, if you're building a game with a large set of systems which need to "tick" every frame Myriad does contain a set of systems.

Clone this wiki locally