EasyEcs

Entities and components

How EasyEcs stores game objects as entities with struct components. Version-checked handles, EntityId references, tags, Set/Get/Read and generated members.

Every object of a server is an entity: a player, an NPC, a projectile, a loot chest. An entity is only an identity; its data lives in components, plain structs stored in tables with one row per entity. A handle to an entity checks its version on every access, so a player who logged out, or a monster that died and whose id went to a new monster, can never be written through an old handle.

When to use it

Use it for

Everything that exists many times and changes over time: units, items, sessions, timers. Put each piece of data in its own small component (Position, Health) and each state in a tag (Dead, Stunned), so systems select exactly the entities they work on.

Skip it for

Data of the whole world (a clock, configuration, a spatial grid): that is a singleton. Behavior inside components: components are data, logic goes into systems. Classes as components: they do not compile (EECS006).

How to use it

Declare components

A component is a struct that implements IComponent. A record struct gives you a constructor, equality and ToString for free. A struct without fields is a tag.

public record struct Position(float X, float Y) : IComponent;
public record struct Health(int Value) : IComponent;
public record struct Armor(int Value) : IComponent, ITrackChanges;   // writes are tracked
public record struct Name(string Value) : IComponent;                // managed fields are allowed
public struct Dead : IComponent;                                     // a tag: no fields

Create entities

CreateEntity takes 0 to 9 component values and places the entity in its final table in one step.

var world = new World();
Entity goblin = world.CreateEntity(new Position(10, 0), new Health(30), new Armor(2));
Entity marker = world.CreateEntity();                          // no components yet

Read, write, add and remove

The generator gives every component named members, so the common calls read like fields. Write through a ref, add or replace with SetX, remove with RemoveX.

goblin.Position.X += 1;                     // a ref into the table: written in place
goblin.SetHealth(25);                       // adds or replaces the value
int armor = goblin.Armor.Value;             // a tracked component: ref readonly, a read is not a write
goblin.SetDead();                           // adds the tag
if (goblin.IsDead) goblin.RemoveHealth();   // removes a component; the entity keeps its row
goblin.Destroy();

Entities

An Entity is a 16-byte handle: the world, and an EntityId (index and version). It holds no data and no behavior. Every member validates the version first:

Call on a destroyed or default handleResult
IsAlive, Has<T>(), TryGet<T>(), TryGetRef<T>()false (no exception)
Get<T>(), Read<T>(), GetOrAdd<T>(), Set(...), Remove<T>(), Clone()InvalidOperationException: "Entity is not alive"
Destroy()ignored

Two handles are equal when they name the same incarnation in the same world. Destroying an entity bumps its version, so every handle to it becomes stale at once, and it never reaches the entity that later reuses the index.

Waves of spawns and clean-ups have bulk forms, each one pass through the structural gate:

var wave = new Entity[500];
world.CreateEntities(wave, new Position(0, 0), new Health(100));   // the same values for each; handles written to wave
int cleared = world.DestroyEntities<Health, Dead>();                 // every dead entity with Health

Kinds of entities spawned again and again (an NPC type, a projectile) are best defined once as a prefab: world.Instantiate(prefab) creates one, entity.Clone() copies a living one.

The world counts and lists its entities:

world.EnsureEntityCapacity(10_000);       // a server that knows its peak never grows the entity records
int alive = world.EntityCount;            // O(1)
foreach (var e in world.Entities)         // diagnostics: walks the whole id range
    Console.WriteLine(e);

WorldOptions.InitialEntityCapacity does the same at construction:

var sized = new World(new WorldOptions { InitialEntityCapacity = 10_000 });

Tables grow on their own; to size a table too, use EnsureCapacity<T1, …, Tn>(count) for entities created with exactly those components, or EnsureCapacity(prefab, count) for a prefab's instances.

Ids, versions and recycling

entity.Id is an EntityId: 8 bytes, an Index and a Version, unmanaged. It is what you store in a component, send to a client or write to a log. world.TryGetEntity(id, out var e) turns it back into a handle when that incarnation is still alive, and a handle converts to its id implicitly.

var target = world.CreateEntity(new Health(100));
EntityId fromClient = target.Id;                       // what a client sends back

if (world.TryGetEntity(fromClient, out var found) && found.HasHealth)
    found.Health.Value -= 10;                          // the current incarnation of that id

target.Destroy();
bool gone = !world.IsAlive(fromClient);                // true: the id names the destroyed incarnation
var reused = world.CreateEntity();                     // may take the same index, with a new version
bool same = reused.Id == fromClient;                   // false
  • Indexes are recycled last in, first out, so recently freed slots are still warm in cache and the entity records stay bounded by the peak population.
  • Versions start at 1. EntityId.Null (default) has version 0 and names no entity; id.IsNull tests it.
  • world.GetEntity(id) throws instead of returning false.

Refer to entities with EntityId

A component that points at another entity (an owner, a target, a parent) stores the EntityId, not the Entity. An Entity holds a reference to its world, so a component with an Entity field becomes a managed column: the GC scans it, every store pays a write barrier, rows are no longer copied as blocks and saves cannot write it. The analyzer warns (EECS017).

public record struct Target(EntityId Id) : IComponent;   // 8 bytes, unmanaged

public class Chase : EcsSystem
{
    [ForEach]
    static void Follow(ref Position p, in Target target, World world)
    {
        if (world.TryGetEntity(target.Id, out var prey)) p = prey.Position;   // gone: the id finds nothing
    }
}

To find every entity that points at a target, make the link an indexed component or a relation: Indexes and relations.

Components

The generic API works for any component type; the generated members are shorthands for it.

OperationEffect
ref T Get<T>()A ref into the table; throws if the entity lacks T. A write of a tracked component.
ref readonly T Read<T>()A read-only ref; throws if missing. Never a write.
ref T TryGetRef<T>(out bool found)A ref if present, else a null ref (check found). A write when found.
bool TryGet<T>(out T value)A copy if present. Never a write.
bool Has<T1..Tn>()Whether the entity has all of them.
Entity Set(in T1 … in Tn)Adds or replaces 1 to 9 values, with at most one row move; chainable.
ref T GetOrAdd<T>()A ref, adding default first if missing; an existing value is kept.
Entity Remove<T1..Tn>()Removes 1 to 9 components; missing ones are ignored; chainable.
var hero = world.CreateEntity(new Position(0, 0), new Health(100), new Name("Ayla"));
hero.Set(new Armor(3), new Position(5, 5));        // one call: shorter than two, same cost
ref var hp = ref hero.TryGetRef<Health>(out bool has);
if (has) hp.Value -= 10;                           // one lookup, written in place
ref var shield = ref hero.GetOrAdd<Armor>();       // kept: Armor(3)
hero.Remove<Armor, Name>();
bool both = hero.Has<Position, Health>();

Always change components through a ref: var hp = hero.Get<Health>() is a copy, and changing it changes nothing in the table (ref and await).

Generated members

The source generator gives Entity, World and Prefab named members for every component and singleton of your project (C# 14 extension members, the .NET 10 default). Each one is an inlined call to the generic API, so both cost the same.

DeclaredGeneratedSame as
record struct Position(float X, float Y) : IComponente.Position (ref), e.HasPosition, e.TryGetPosition(out p), e.GetOrAddPosition(), e.SetPosition(x, y), e.RemovePosition()Get, Has, TryGet, GetOrAdd, Set(new Position(x, y)), Remove
a tracked component (Armor : ITrackChanges)e.Armor is ref readonly; e.WriteArmor() returns the refRead; Get (stamped as written)
an indexed component (Zone : IIndexed<int>)e.Zone is ref readonly, no GetOrAddZone or WriteZone; world.ZoneIndexRead; world.Index<Zone, int>()
a grouped component (Team : IGroup)e.Team is ref readonly, no GetOrAddTeam or WriteTeam; filter.InTeam(group)Read; filter.InGroup<Team>(group) (groups)
a tag (struct Dead : IComponent)e.IsDead, e.SetDead(), e.RemoveDead()Has, Set(new Dead()), Remove
any component with fields, except a cleanup componente.DisableHealth(), e.EnableHealth(), e.IsHealthDisabledDisable / Enable
a buffer element some code names as DynamicBuffer<Loot>e.Loot (ref readonly), e.HasLoot, e.WriteLoot(), e.GetOrAddLoot(), e.RemoveLoot()Read, Has, Get, GetOrAdd, Remove of DynamicBuffer<Loot> (dynamic buffers)
a relation (MemberOf : IRelation), once code names DynamicBuffer<MemberOf>e.AddMemberOf(target), e.RemoveMemberOf(target), e.LinksMemberOf(target), e.MemberOf (ref readonly), e.HasMemberOf, e.RemoveMemberOf(), world.MemberOfIndexAddRelation, RemoveRelation, HasRelation, Read, Has, Remove, world.Relations<MemberOf>() (relations)
a singleton (Clock : ISingletonComponent)world.Clock (ref), world.HasClock, world.GetOrAddClock(), world.SetClock(…), world.RemoveClock()GetSingleton, HasSingleton, GetOrAddSingleton, GetOrAddSingleton() = …, RemoveSingleton
each world.Query<Position, Velocity>() the code callsitem.Position, item.Velocityitem.Component1, item.Component2
any component, on a prefabprefab.Position (ref readonly), HasPosition, SetPosition(x, y), RemovePosition(), IsDead, SetDead()Read, Has, Set, Remove
hero.Position.X += 1;                                      // ref: written in place
if (hero.TryGetHealth(out var copy)) Console.WriteLine(copy.Value);
hero.SetArmor(5).SetDead();                                // constructor arguments; a tag; chainable
int value = hero.Armor.Value;                              // tracked: a read, never stamped
hero.WriteArmor().Value += 1;                              // an explicit write: stamped
  • The property is Get (or Read), so it throws when the component is missing: check HasX, or use TryGetX / TryGetRef<T>(out found) when it is optional.
  • SetX has one overload per public constructor (a record struct's primary constructor), or one that takes every field when the struct has no constructor and only public, writable fields.
  • Writes are methods, not property setters: C# rejects a setter on a handle that is not a variable, such as a foreach variable.
  • The analyzer knows these members: SetX, GetOrAddX and RemoveX are structural changes, the rest are reads or writes of a value.
  • No members are generated for generic components, components nested in types other code cannot see, and names the receiver already has (Entity.Id, World.Update); the generic API still works for them.

Tags

A tag is a component without fields: public struct Dead : IComponent;. It costs no column and exists to be filtered on ([Without<Dead>], QueryFilter.Create().With<Stunned>()). Use tags for states that come and go (dead, stunned, burning). Removing a tag and adding it back flips one presence bit in the entity's row, so a state that toggles every few ticks moves no data. For a value that changes every tick, a field is still cheaper: ECS principles, rule 5.

Where components live

Components live in archetype tables, one row per entity and one column per component type.

  • Removing a component keeps the row. The column's presence bit for that row is cleared; adding the component back sets it again. Nothing moves.
  • Gaining a component the table has no column for moves the row to the table with that column, once per new combination. The move plan is cached per source table.
  • Managed values are cleared on removal (and when the entity is destroyed), so the GC can collect what they referenced. Unmanaged values are left in place: the next Set or GetOrAdd overwrites them.
  • Up to 65,536 component types per process. The first 256 fit in each table's mask inline; past that, a mask carries an overflow array.

Full example

A goblin and a chest: create them, write through the generated members, mark a state with a tag, read an optional component, then destroy the goblin and check its id. Put the files in a console project that references EasyEcs (dotnet add package EasyEcs) and run it with dotnet run.

It prints:

goblin: Position { X = 3, Y = 0 }, Health { Value = -10 }, armor 3
goblin dead: True, chest dead: False
chest: no health, nothing to hit
0v1 alive: False, entities left: 1

goblin.Position.X += 3 wrote the table in place, while the chest, which has no Health, answered TryGetHealth with false instead of throwing. After Destroy the stored id names nothing: the world says it is not alive.

How to use it fast

Instead ofDoWhy
CreateEntity() then Set per componentCreateEntity(a, b, c), or Instantiate(prefab)One placement instead of one row move per component
Set(a) then Set(b)Set(a, b), Remove<A, B>()Shorter, the same cost
A loop of CreateEntity / DestroyCreateEntities(span, …), DestroyEntities<T>(filter) (EECS031)One pass through the structural gate per wave
Get<T>() to read a tracked componentRead<T>(), the generated property, TryGet<T>()A ref access stamps the row as written
Has<T>() then Get<T>()TryGetRef<T>(out found)One lookup
Several Get / TryGetRef and a Has<Dead>() per requestentity.Run with a static readonly filterOne lookup for every component; the refs survive structural changes
An Entity field in a componentAn EntityId fieldUnmanaged column: no GC scan, block copies

Per-entity access through handles, 1,000 handles each (MicroBenchmarks: HandleAccess writes Position through the handle, TryGetEntity looks the id up first, TrackedHandleWrite writes a tracked component):

CaseMeanvs baselineAllocated
HandleAccess3.33 μs0.26×0 B
TryGetEntity3.60 μs0.28×0 B
TrackedHandleWrite8.37 μs0.66×0 B
BenchmarkDotNet 0.15.2, .NET 10.0.13, Intel Xeon Processor 2.10GHz, 4 cores, Ubuntu 24.04.5 LTS.

Creating and destroying in a warm world, 1,000 entities ten times over: one call per entity (Churn) against CreateEntities and DestroyEntities (ChurnBatch); and moving three components out and back in one call each (RemoveMany, 1,000 entities):

CaseMeanvs baselineAllocated
Churn334 μs26×0 B
ChurnBatch157 μs12×0 B
RemoveMany51.0 μs4.0×0 B
BenchmarkDotNet 0.15.2, .NET 10.0.13, Intel Xeon Processor 2.10GHz, 4 cores, Ubuntu 24.04.5 LTS.

Against Friflo.Engine.ECS on the same mixed world: spawning and destroying 5,000 projectiles, and stunning 5% of the fighters then lifting it (two structural changes each):

Structural changes per operation, 100,000 entities

  • EasyEcs
  • Friflo.Engine.ECS
CreateDestroyFriflo.Engine.ECS 1.13×190 μs · 0 B215 μs · 0 BToggleFriflo.Engine.ECS 1.94×517 μs · 0 B1.00 ms · 0 B0.00 ns200 μs400 μs600 μs800 μs1.00 msTime (shorter is faster)

BenchmarkDotNet 0.15.2, .NET 10.0.13, Intel Xeon Processor 2.10GHz, 4 cores, Ubuntu 24.04.5 LTS.

Data table
LibraryCaseMeanvs EasyEcsAllocated
EasyEcsCreateDestroy190 μs1.00×0 B
Friflo.Engine.ECSCreateDestroy215 μs1.13×0 B
EasyEcsToggle517 μs1.00×0 B
Friflo.Engine.ECSToggle1.00 ms1.94×0 B

What it costs

  • Per entity: a 16-byte record (table, row, version) outside the tables, plus its row.
  • A handle is 16 bytes; an EntityId 8 bytes.
  • Get, Read, Has through a handle: the entity's record, then one cache line of the table's lookup. O(1).
  • Create: O(components), amortized; a new combination of components creates its table once.
  • Destroy: O(components); outside a pass the table's last row fills the hole at once.
  • Remove a component, add it back: one presence bit. Gaining a new column: one row copy.
  • Writes of untracked components cost nothing extra; tracked ones store one tick (change tracking).
  • After warm-up, none of this allocates.

Pitfalls

MistakeWhat happensCaught by
A class implementing IComponentDoes not compileEECS006
An Entity field in a componentA managed column, scanned by the GC, not savedEECS017 (warning)
Changing a copy: var hp = e.Get<Health>(); hp.Value--;The table is unchangedCode review: ref var hp = ref …
Storing id.Index aloneNames whichever entity reuses the indexStore the EntityId; TryGetEntity checks the version
Reading a tracked component with Get<T>()The row counts as written, every [Changed<T>] reader visits itEECS025; the generated property is ref readonly
Writing an indexed component through Get<T>()The index keeps the old keyEECS015
A DynamicBuffer<T> copied out of an entityTwo entities share one listEECS012
Get<T>() on a component the entity may lackInvalidOperationExceptionUse HasX, TryGetX or TryGetRef
Edit on GitHub

Last updated on

How is this guide?

On this page