Skip to content

Class ComponentPool<T> ​

Namespace: KenseiECS

Sparse Set — storage for components of a single type.

Layout:

text
sparse[entityIndex] → denseIndex       (O(1) lookup by entity)
dense[denseIndex]   → entityIndex       (reverse mapping)
data[denseIndex]    → T                 (component data)

Dense array has no gaps — ideal for linear iteration. Removal via swap-remove: last element takes the place of the removed one. Notifies World on Add/Remove so filters stay up to date.

csharp
public sealed class ComponentPool<T> : ComponentPoolBase where T : struct, IComponent

Type Parameters ​

T

Inheritance ​

object ← ComponentPoolBase ← ComponentPool<T>

Inherited Members ​

ComponentPoolBase.TypeIndex, ComponentPoolBase.ComponentType, ComponentPoolBase.Count, ComponentPoolBase.RawEntities, ComponentPoolBase.SparseCapacity, ComponentPoolBase.DenseCapacity, ComponentPoolBase.ReservedComponents, ComponentPoolBase.ComponentSize, ComponentPoolBase.AllocatedBytes, ComponentPoolBase.Has(int), ComponentPoolBase.Remove(int), ComponentPoolBase.Reserve(int), object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Properties ​

ComponentSize ​

Size of one component in bytes, or 0 when it holds references and cannot be measured.

csharp
public override int ComponentSize { get; }

Property Value ​

int

RawData ​

Dense data array — for linear iteration in systems. Valid range is 0..Count.

csharp
public T[] RawData { get; }

Property Value ​

T[]

TracksChanges ​

Whether this pool records a change version per component.

csharp
public bool TracksChanges { get; }

Property Value ​

bool

Methods ​

Add(int, T) ​

Add component. O(1). Returns ref to the added component. Throws if the entity already has it. The ref has the same validity as Get. If a listener removes the component again or destroys the entity before Add returns, the ref points at a scratch value and writes through it are discarded, even when a new entity has taken the slot; if a listener removed the component and added it to the same entity again, the ref points at that new component.

csharp
public ref T Add(int entityIndex, T value)

Parameters ​

entityIndex int

value T

Returns ​

T

AddListener(IComponentListener<T>) ​

Register a typed listener notified on Add (after filters update) and Remove (before AutoReset). It hears the changes that happen after it subscribed: registered by a filter or world listener while an Add of this pool is running, it gets no OnAdded for that Add.

csharp
public void AddListener(IComponentListener<T> listener)

Parameters ​

listener IComponentListener<T>

ChangedSince(int, int) ​

True when the component was added or modified after the given world.ChangeVersion, also when the world's counter has passed int.MaxValue and started again in between (see ChangeVersion for how long a bookmark stays exact then).

csharp
public bool ChangedSince(int entityIndex, int version)

Parameters ​

entityIndex int

version int

Returns ​

bool

ChangedVersion(int) ​

Change version recorded for the component (from Add, Modify or MarkChanged). Requires TrackChanges. Compare it with ChangedSince(), not with >: the world's counter starts again at 1 after int.MaxValue, and from then on the version of a component that was not written for more than 2^30 counted writes is moved up to stay that old (see ChangeVersion).

csharp
public int ChangedVersion(int entityIndex)

Parameters ​

entityIndex int

Returns ​

int

Get(int) ​

Get component by ref. O(1). ref return is critical for struct components — avoids copying. The ref is valid until the next Add or Remove of this component type on any entity: Add may grow the dense array, Remove swaps the last component into the freed slot. If this pool belongs to an owning group, an Add or Remove of any of the group's types also invalidates it — the group reorders the dense arrays of all its pools.

csharp
public ref T Get(int entityIndex)

Parameters ​

entityIndex int

Returns ​

T

MarkChanged(int) ​

Mark the component changed without reading it.

csharp
public void MarkChanged(int entityIndex)

Parameters ​

entityIndex int

Modify(int) ​

Get component by ref and mark it changed. Use instead of Get when writing to a tracked component. The ref has the same validity as Get.

csharp
public ref T Modify(int entityIndex)

Parameters ​

entityIndex int

Returns ​

T

Remove(int) ​

Remove component. O(1) via swap-remove. No-op if the entity does not have it. Last dense element moves to the removed slot, keeping the array dense. A typed listener may remove the same component again from OnRemoved (for example by destroying its entity), also while removing another component of this type (linked entities that remove each other): the component is removed once and every listener still gets exactly one OnRemoved for it. A component added to the entity again from OnRemoved, or to a new entity that took the slot, is a new component: this Remove does not touch it.

csharp
public override void Remove(int entityIndex)

Parameters ​

entityIndex int

RemoveListener(IComponentListener<T>) ​

Unregister a typed listener.

csharp
public void RemoveListener(IComponentListener<T> listener)

Parameters ​

listener IComponentListener<T>

TrackChanges() ​

Start recording change versions. Existing components count as changed now. Costs one int per component and one store per Add, Remove and Modify.

csharp
public void TrackChanges()