EasyEcs

Prefabs and cloning

Define a kind of entity once as a Prefab and spawn it alone, in waves or as a linked group: derived prefabs, prefab buffers, EnsureCapacity and Entity.Clone.

A server spawns the same kinds of entities again and again: a goblin, a fireball, a loot chest, a boss with its guards. A Prefab is the recipe of such a kind, a set of component values that belongs to no world, so no query or system ever sees it. world.Instantiate(prefab) creates an entity with a copy of every value, and entity.Clone() does the same from a living entity.

When to use it

Use it for

Kinds of entities defined once and spawned many times: NPC types, projectiles, items, read from code or from configuration at startup. Waves of spawns (Instantiate(prefab, span)). Groups that always come together (a boss, its mount and its guards). Clone for a runtime copy of a living entity: a mirror image, a slime that splits.

Skip it for

One-off entities whose values differ every time: CreateEntity(a, b, c) is as fast and says more. A prefab someone changes while another thread instantiates it. An Entity field meant to point at an entity of the target world: give each instance its links after instantiating.

How to use it

Build the recipe

The generated writers (SetHealth, SetNpc, ...) exist on Prefab too, and return the prefab, so a recipe is one chain. Keep prefabs in static readonly fields or a table of kinds.

Kinds.cs
public record struct Position(float X, float Y) : IComponent;
public record struct Health(int Value, int Max) : IComponent, ITrackChanges;
public record struct Attack(int Damage, int Cooldown) : IComponent;
public struct Npc : IComponent;                                         // a tag

public static class Kinds
{
    public static readonly Prefab Goblin = new Prefab()
        .SetPosition(0, 0).SetHealth(60, 60).SetAttack(4, 0).SetNpc();
    public static readonly Prefab Troll = new Prefab()
        .Set(new Position(), new Health(300, 300), new Attack(12, 0)).SetNpc();   // or several values in one call
}

Instantiate one

The new entity gets a copy of every value. Write only what differs.

var world = new World();
var goblin = world.Instantiate(Kinds.Goblin);
goblin.Position = new Position(100, 40);

Instantiate a wave

Instantiate(prefab, span) fills the span with new entities: one pass through the structural gate, the prefab's row copied into consecutive rows at once, at most one table growth.

var wave = new Entity[500];
world.Instantiate(Kinds.Troll, wave);
for (int i = 0; i < wave.Length; i++) wave[i].Position = new Position(i * 8, 0);

The Prefab API

MemberEffect
new Prefab()An empty recipe.
new Prefab(basePrefab)A copy of basePrefab (and its members) to change on its own.
Set(in T1 … in Tn) → PrefabAdds 1 to 9 values, or replaces the values of their types.
Remove<T1..Tn>() → PrefabDrops 1 to 9 components; missing ones are ignored.
Has<T>()Whether the prefab has T.
Read<T>() → ref readonly TThe prefab's value; throws if it has none.
SetBuffer<T>(params ReadOnlySpan<T>) → PrefabA DynamicBuffer<T> with these elements.
AddMember() → PrefabA new member of the prefab's group.
IdThe EntityId that names this member inside the group's components.

Generated members

Like an entity, a prefab gets named members for every component of your project:

DeclaredGenerated on PrefabSame as
record struct Health(int Value, int Max) : IComponentprefab.Health (ref readonly), prefab.HasHealth, prefab.SetHealth(value, max), prefab.RemoveHealth()Read, Has, Set(new Health(value, max)), Remove
a tag (struct Npc : IComponent)prefab.IsNpc, prefab.SetNpc(), prefab.RemoveNpc()Has, Set(new Npc()), Remove
a buffer element (record struct Loot(int Item))prefab.SetLoot(new Loot(1), new Loot(2)), prefab.HasLoot, prefab.Loot (ref readonly), prefab.RemoveLoot()SetBuffer(…), Has, Read, Remove of DynamicBuffer<Loot>
var elite = new Prefab().SetPosition(0, 0).SetHealth(500, 500).SetAttack(20, 0).SetNpc();
int max = elite.Health.Max;                          // read-only: change it with SetHealth
if (elite.HasAttack) elite.SetAttack(25, 0);         // replaces the value
elite.RemoveNpc();                                   // the recipe drops the tag

The property is always read-only: a prefab changes only through Set, Remove and their generated forms, so there is no TryGetX, GetOrAddX or WriteX on a prefab. A prefab belongs to no world, so its writers are not structural changes of any world, and the analyzer does not report them inside [ForEach] methods or on other threads.

Derived prefabs

new Prefab(basePrefab) copies a prefab, members included, into a new one that changes on its own: a variant of a configured NPC. It is a copy, not inheritance: later changes of either prefab do not reach the other.

var chief = new Prefab(Kinds.Goblin).SetHealth(200, 200).SetAttack(9, 0);   // a goblin, stronger
var boss = world.Instantiate(chief);

Buffers in a prefab

A prefab can carry a DynamicBuffer<T>: the generated SetLoot(...) (or SetBuffer<T>(...)) gives it these elements, and every instance gets a list of its own with a copy of them. The element's members (SetLoot, entity.Loot, WriteLoot) are generated once some code of the project names DynamicBuffer<Loot>.

public record struct Loot(int Item, int Count);

public static class Chests
{
    public static readonly Prefab Common = new Prefab().SetPosition(0, 0).SetLoot(new Loot(1, 3), new Loot(7, 1));

    public static int Items(Entity chest) => chest.Read<DynamicBuffer<Loot>>().Count;   // names the buffer type
}
var chest = world.Instantiate(Chests.Common);
ref var items = ref chest.WriteLoot();               // this chest's own list
items.Add(new Loot(9, 1));
int inRecipe = Chests.Common.Loot.Count;             // still 2

Do not Set(new DynamicBuffer<T>()) and fill it outside the prefab: a buffer value copied around shares its list (EECS012).

Prefab groups

Some kinds always come together: a boss with its mount and its guards, a squad, a vehicle and its seats. Give the prefab members with AddMember(); each member is a prefab of its own. Instantiating the prefab, or any member, creates the whole group and returns the entity of the one you passed.

Inside a group, an EntityId field that names a member (its Id) is rewritten to that member's new entity in every instance, so links survive the copy. Any other id becomes EntityId.Null: ids in a prefab name members only.

public record struct Follows(EntityId Target) : IComponent;

public static class Bosses
{
    public static readonly Prefab Warlord = Build();

    static Prefab Build()
    {
        var warlord = new Prefab().SetHealth(5_000, 5_000).SetNpc();
        var mount = warlord.AddMember().SetHealth(800, 800).Set(new ChildOf(warlord.Id));
        warlord.AddMember().SetHealth(300, 300).Set(new ChildOf(warlord.Id), new Follows(mount.Id));
        return warlord;
    }
}
var warlord = world.Instantiate(Bosses.Warlord);     // three entities, linked to each other
int guards = world.Index<ChildOf, EntityId>().Count(warlord);   // 2
warlord.Destroy();                                   // ChildOf: destroying the parent destroys its children

ChildOf is EasyEcs's hierarchy link: see Hierarchy. Instantiate(group, span) instantiates the group once per element.

Reserve before the wave: EnsureCapacity

world.EnsureCapacity(prefab, count) grows the table the prefab's instances go to, and the entity records, so the next count instances grow nothing. For a group, it reserves every member's table. Call it before the tick that spawns, so the wave pays no growth inside it.

world.EnsureCapacity(Kinds.Troll, 5_000);
var army = new Entity[5_000];
world.Instantiate(Kinds.Troll, army);                // no growth, no allocation

What a copy gets

Instantiate and Clone copy every component value as it is:

  • Values are copied like any struct. A managed reference inside a component (a string, an array, a List<T>) is copied as a reference, so the copies share what it points to.
  • A DynamicBuffer<T> is the exception: every copy gets a list of its own with the same elements.
  • Value indexes learn the new entity at once.
  • Tracked components count as written now: [Changed<T>] methods see every new instance. A component the copy lacks is not a removal: [Removed<T>] does not report it.
  • Absent components stay absent. A clone of an entity whose Burning was removed has no Burning.
  • An Entity field is copied as the handle it holds. In a prefab it names no entity of the target world, so give each instance its links after instantiating, or use an EntityId to a member.

Cloning a living entity

entity.Clone() creates a new entity of the same world with the entity's current components, under the same rules. Use it for runtime copies of a live entity's state: a mirror image, a slime that splits in two, a player's state taken for a duel replay. For kinds of entities, use a prefab: it does not depend on some entity that may change or die. Clone throws for a handle that is not alive.

public static class Mirrors
{
    public static Entity Mirror(Entity unit) => unit.IsAlive ? unit.Clone() : default;   // health, statuses, bag
}

Why a prefab is not an entity

Some libraries keep templates as entities of the world: Unity Entities and Flecs tag them, and every query excludes that tag; Friflo clones an ordinary entity of the store, which its queries see like any other unless it is disabled. Either way the templates live among the entities that systems move, damage and replicate. In EasyEcs a Prefab is data outside every world: no query can see it, systems need no filter for it, and one prefab serves every world (several threads may instantiate it at once as long as nobody changes it meanwhile).

Full example

Two kinds of enemy, the second derived from the first, spawned one at a time, as a wave and by cloning. Put the files in a console project that references EasyEcs (dotnet add package EasyEcs) and run it with dotnet run.

It prints:

103 enemies
first at Position { X = 10, Y = 20 }
boss 50 hp, twin 50 hp
103 entities in the world

The twin has the boss's 50 health, not the chief's 80: a clone copies the entity, not its prefab. The two prefabs are not among the 103 entities.

How to use it fast

Instead ofDoWhy
Instantiate in a loop for a waveInstantiate(prefab, span)One gate pass, consecutive rows copied at once
Growing tables during the spawning tickEnsureCapacity(prefab, count) before itThe wave allocates nothing
CreateEntity then Set per component of a kindA prefabOne row copy, the target table cached per prefab
A new Prefab per spawnOne prefab per kind, kept in a fieldIts table lookup is cached after the first instance
Templates as hidden entitiesA PrefabNo query pays to skip them

Spawning 1,000 entities of four components (one tracked) and destroying them, one Instantiate per entity against one Instantiate(prefab, span) (PrefabBenchmark):

CaseMeanvs baselineAllocated
InstantiateEach49.7 μs1.00×0 B
InstantiateBatch15.3 μs0.31×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, which clones a template entity of its store: 5,000 NPCs of six components spawned and destroyed:

Spawn 5,000 NPCs from a template and destroy them

  • EasyEcs
  • Friflo.Engine.ECS
InstantiateFriflo.Engine.ECS 1.68×164 μs · 0 B276 μs · 0 B0.00 ns50.0 μs100 μs150 μs200 μs250 μsTime (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
EasyEcsInstantiate164 μs1.00×0 B
Friflo.Engine.ECSInstantiate276 μs1.68×0 B

What it costs

  • A prefab is a small private world with one row per member, allocated once when you build it.
  • Instantiate: one row copy, O(components). The first instance finds the target table; the world caches it per prefab table, so later instances go straight to it.
  • Instantiate(prefab, span): O(count × components) with one gate pass and at most one table growth.
  • A group: O(members × components) per instance, plus the rewriting of member ids.
  • Clone: one row copy within the entity's own table.
  • Allocations: none after warm-up, except the list each DynamicBuffer<T> copy gets.

Pitfalls

MistakeWhat happensCaught by
An Entity field in a prefab's componentEvery instance points at the same handle, of no worldEECS017 (warning); use an EntityId to a member
An EntityId of a world's entity in a prefabBecomes EntityId.Null in every instanceSet links after instantiating
Filling a DynamicBuffer<T> value before SetEvery instance shares that listEECS012; use SetLoot(...)
Instantiate(prefab) in a counted loopOne copy per call instead of one waveEECS031: Instantiate(prefab, span)
Instantiate on a thread that does not own the worldRaces with the ownerEECS014
Instantiate in a [ForEach] method without Entity or WorldThe pass assumes no structural changeEECS013
Changing a prefab while other threads instantiate itTorn copiesBuild prefabs at startup, then only read them
Clone() on a destroyed entityInvalidOperationExceptionCheck IsAlive
Edit on GitHub

Last updated on

How is this guide?

On this page