Skip to content

Components ​

All components must be structs implementing IComponent:

csharp
struct Health : IComponent { public float Value; }

Access ​

Through the world, by Entity handle:

csharp
world.Add(entity, new Health { Value = 100 });   // throws if already present
ref var hp = ref world.Get<Health>(entity);      // by ref, no copy
bool has = world.Has<Health>(entity);            // does not create the pool
world.Remove<Health>(entity);                    // no-op if absent

Removing the last component destroys the entity (see auto-destroy).

Through the pool, by int index. Cache pools in Init; filter loops give you the index directly:

csharp
// Pool access (cache in Init)
var pool = world.Pool<Health>();
ref var hp = ref pool.Get(entity.Index);

Pool access is unchecked in release

ComponentPool<T>.Get(int) on an entity without the component reads garbage in release and throws under KENSEI_DEBUG. See Release vs. KENSEI_DEBUG.

Ref lifetime ​

A ref T from Get points into the pool's dense array, not at the entity. Treat it as invalid after any structural change of that pool:

  • an Add of the same component type to any entity: the dense array may grow and move;
  • a Remove of the same component type from any entity, including DestroyEntity and one-frame cleanup: swap-remove moves the last element into the freed slot, so a ref to that last element now points past the live range and writes through it are lost;
  • pool.Reserve(count) (or the obsolete world.Reserve(entities, components)) when it grows the pool's dense arrays: like any growth, it moves them.

Re-acquire the ref after structural changes.

csharp
ref var hp = ref world.Get<Health>(entity);
world.Add(other, new Health());   // may reallocate the Health pool
hp.Value = 10;                    // may write into the old array — re-Get instead

hp = ref world.Get<Health>(entity);   // re-acquired after the structural change
hp.Value = 10;                        // safe

Changes to other component types do not affect it, except in an owning group: there an Add or Remove of any of the group's types reorders the dense arrays of all its pools, so it invalidates refs into every one of them.

IAutoReset ​

Custom cleanup when a component is removed:

csharp
struct Inventory : IComponent, IAutoReset<Inventory> {
    public List<int> Items;
    public void AutoReset(ref Inventory c) {
        c.Items?.Clear();
        c.Items = null;
    }
}

Components without IAutoReset are reset to default(T) automatically. AutoReset is called for the removed component only; the component moved into its slot by swap-remove is untouched. Warmup and Clear also call it, so it must handle default(T).

The bridge is an ordinary interface call on one boxed default(T) the pool keeps, AOT/IL2CPP-safe: one boxing allocation per pool at construction, zero allocations per Remove (the component is passed by ref). Explicit interface implementations are supported. No reflection is involved, so Unity's managed code stripping, .NET trimming and NativeAOT keep your implementations at every level (up to 2.4.0 the pool found them through reflection, and Medium and High removed them: see installation).

IAutoCopy ​

Custom deep-copy logic for CopyEntity:

csharp
struct Inventory : IComponent, IAutoCopy<Inventory> {
    public List<int> Items;
    public void AutoCopy(ref Inventory c) {
        c.Items = c.Items != null ? new List<int>(c.Items) : null;
    }
}

AutoCopy is called on a shallow copy of the source component, before it is stored on the new entity; replace the reference fields there. Components without IAutoCopy are copied by value (shallow copy). EventBuffer<T> and Listeners<T> implement it, so a copied entity gets its own list.

If a listener has already given the new entity that component during CopyEntity, the copy overwrites it, and change tracking counts the overwrite. For a type with IAutoCopy, AutoReset runs on the overwritten value first. Without IAutoCopy it does not run, since that value may share references with the source.

Component types ​

Every component type gets a process-wide index (ComponentType<T>.Index). The index resolves back to the type:

csharp
var types = new List<int>();
world.GetComponentTypes(entity, types);
foreach (int typeIndex in types) {
    Console.WriteLine(ComponentType.NameOf(typeIndex));
    var pool = world.GetPool(typeIndex);   // ComponentPoolBase
}

int healthIndex = ComponentType<Health>.Index;
Type healthType = ComponentType.TypeOf(healthIndex);   // typeof(Health)

GetPool(int) returns the untyped ComponentPoolBase, or null if the world has no pool for that type yet.

WARNING

Indices depend on first-touch order and are not stable across runs; do not persist them.

See also ​