Skip to content

Class ComponentPoolBase ​

Namespace: KenseiECS

Untyped part of a component pool: the sparse set of entity indices. World stores pools of every component type as this base so it can walk masks and remove components without knowing T. Operations that bypass World bookkeeping (Clear, AddDefault, CopyTo) are internal — calling them directly would desynchronize masks and filters.

csharp
public abstract class ComponentPoolBase

Inheritance ​

object ← ComponentPoolBase

Derived ​

ComponentPool<T>

Inherited Members ​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Properties ​

AllocatedBytes ​

Approximate bytes held by this pool's arrays.

csharp
public long AllocatedBytes { get; }

Property Value ​

long

ComponentSize ​

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

csharp
public abstract int ComponentSize { get; }

Property Value ​

int

ComponentType ​

Component type stored in this pool.

csharp
public Type ComponentType { get; }

Property Value ​

Type

Count ​

Number of components in the pool.

csharp
public int Count { get; }

Property Value ​

int

DenseCapacity ​

Length of the dense arrays.

csharp
public int DenseCapacity { get; }

Property Value ​

int

RawEntities ​

Dense entity index array — parallel to the typed data array. Valid range is 0..Count.

csharp
public int[] RawEntities { get; }

Property Value ​

int[]

ReservedComponents ​

Components this pool is reserved for: the highest count passed to Reserve(), 0 while it was not reserved (the obsolete Reserve() does not count). A reserved pool covers ReservedEntities with its sparse array, and the filters whose Inc types are all reserved take their reserve from it (ReservedCount).

csharp
public int ReservedComponents { get; }

Property Value ​

int

SparseCapacity ​

Length of the sparse array (grows with the highest entity index that ever had this component).

csharp
public int SparseCapacity { get; }

Property Value ​

int

TypeIndex ​

Unique type index of the component stored in this pool.

csharp
public int TypeIndex { get; }

Property Value ​

int

Methods ​

Has(int) ​

Check if entity has this component. O(1).

csharp
public bool Has(int entityIndex)

Parameters ​

entityIndex int

Returns ​

bool

Remove(int) ​

Remove component. O(1) via swap-remove. No-op if the entity does not have it.

csharp
public abstract void Remove(int entityIndex)

Parameters ​

entityIndex int

Reserve(int) ​

Reserve this pool for the peak of its type: count components fit without further growth, on any entity slot below ReservedEntities (or below count, when that is larger). The dense arrays (entities, data, change versions when tracked) get exactly count slots and the sparse array covers the reserved entity slots — also after a later ReserveEntities(), which extends it. Every filter whose Inc types are now all reserved is reserved with them (ReservedCount). Never shrinks; a repeated call only raises ReservedComponents. Allocates; call at load time.

csharp
public void Reserve(int count)

Parameters ​

count int

Number of components to have room for. 0 does nothing.

Remarks ​

Reserve the types whose count changes during play. A type that is not reserved costs nothing up front and grows on demand: its first component on a high entity index grows its sparse array to that index, a larger count grows its dense arrays. A singleton created at load needs no reserve.

The cost is count × (4 bytes + the component size, + 4 when change versions are tracked) for the dense side and 4 bytes per reserved entity slot for the sparse side, plus 4 bytes per slot and per matching entity for each filter it completes.

Like growth during Add, a Reserve that grows the dense arrays moves the components to new arrays: a ref from Get/Add/Modify and the RawData and [`RawEntities`](KenseiECS.ComponentPoolBase.md#KenseiECS_ComponentPoolBase_RawEntities) arrays taken before it no longer reach the pool, and neither does a [`Entities`](KenseiECS.Filter.md#KenseiECS_Filter_Entities) span of a filter it grows. A running foreach over a filter is not affected. If an allocation fails (`OutOfMemoryException`), the pool keeps working (with arrays grown before the failure) and its reserve, and that of the filters over its type, is not raised. Under KENSEI_DEBUG growth past the reserve is counted in [`GrowthsAfterReserve`](KenseiECS.World.md#KenseiECS_World_GrowthsAfterReserve).

Exceptions ​

ArgumentOutOfRangeException

When count is negative.

ObjectDisposedException

When the pool's world was destroyed.

OutOfMemoryException

When an array of the pool or of a filter it reserves cannot be allocated; the pool keeps working and its reserve is not raised, so a repeated call tries again.