Table of Contents

Class Archetype

Namespace
KeenEyes
Assembly
KeenEyes.Core.dll

An archetype stores all entities that share the same set of component types. Entities are stored in fixed-size chunks for cache-friendly iteration and reduced GC pressure.

public sealed class Archetype : IDisposable
Inheritance
Archetype
Implements
Inherited Members

Remarks

Each archetype maintains a list of ArchetypeChunk instances, each holding up to a fixed number of entities. This chunked storage enables:

  • Cache-friendly iteration (chunks sized for L2 cache)
  • Chunk pooling for reduced allocations
  • Potential parallel processing of different chunks

When an entity's component set changes (via Add or Remove), the entity must migrate to a different archetype. The ArchetypeManager handles these transitions.

Properties

ChunkCount

Gets the number of chunks in this archetype.

public int ChunkCount { get; }

Property Value

int

Chunks

Gets all chunks in this archetype.

public IReadOnlyList<ArchetypeChunk> Chunks { get; }

Property Value

IReadOnlyList<ArchetypeChunk>

ComponentTypes

Gets the component types in this archetype.

public IReadOnlyList<Type> ComponentTypes { get; }

Property Value

IReadOnlyList<Type>

Count

Gets the number of entities in this archetype.

public int Count { get; }

Property Value

int

Entities

Gets all entities in this archetype.

public IEnumerable<Entity> Entities { get; }

Property Value

IEnumerable<Entity>

Id

Gets the unique identifier for this archetype.

public ArchetypeId Id { get; }

Property Value

ArchetypeId

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

GetByEntity<T>(Entity)

Gets a reference to a component for an entity.

public ref T GetByEntity<T>(Entity entity) where T : struct, IComponent

Parameters

entity Entity

The entity to get the component for.

Returns

T

A reference to the component data.

Type Parameters

T

The component type.

GetChunkReadOnlySpan<T>(int)

Gets a readonly span of components from a specific chunk.

public ReadOnlySpan<T> GetChunkReadOnlySpan<T>(int chunkIndex) where T : struct, IComponent

Parameters

chunkIndex int

The chunk index.

Returns

ReadOnlySpan<T>

A readonly span over component values in that chunk.

Type Parameters

T

The component type.

GetChunkSpan<T>(int)

Gets a span of components from a specific chunk for efficient iteration.

public Span<T> GetChunkSpan<T>(int chunkIndex) where T : struct, IComponent

Parameters

chunkIndex int

The chunk index.

Returns

Span<T>

A span over component values in that chunk.

Type Parameters

T

The component type.

GetEntity(int)

Gets the entity at the specified global index.

public Entity GetEntity(int globalIndex)

Parameters

globalIndex int

The global index within this archetype.

Returns

Entity

The entity at the specified index.

Remarks

The global index is sparse across chunk boundaries; see Get<T>(int). To iterate live entities, walk Chunks using each chunk's Count instead of a dense global range.

GetEntityIndex(Entity)

Gets the global index of an entity in this archetype.

public int GetEntityIndex(Entity entity)

Parameters

entity Entity

The entity to locate.

Returns

int

The global index of the entity, or -1 if the entity is not found in this archetype.

Remarks

The returned index is sparse across chunk boundaries; see Get<T>(int). It is derived on demand from the authoritative per-entity location map.

GetEntityLocation(Entity)

Gets the entity location (chunk index and index within chunk).

public (int ChunkIndex, int IndexInChunk) GetEntityLocation(Entity entity)

Parameters

entity Entity

The entity to locate.

Returns

(int ChunkIndex, int IndexInChunk)

A tuple containing the chunk index and index within the chunk, or (-1, -1) if the entity is not found.

GetReadOnlySpan<T>()

Gets a readonly span of components. Note: With chunked storage, this only returns the first chunk's span.

[Obsolete("Use GetChunkReadOnlySpan for chunked archetypes. This method only returns the first chunk. Will be removed in v1.0.")]
public ReadOnlySpan<T> GetReadOnlySpan<T>() where T : struct, IComponent

Returns

ReadOnlySpan<T>

A readonly span over component values in the first chunk.

Type Parameters

T

The component type.

GetReadonly<T>(int)

Gets a readonly reference to a component.

public ref readonly T GetReadonly<T>(int globalIndex) where T : struct, IComponent

Parameters

globalIndex int

The global index of the entity within this archetype.

Returns

T

A readonly reference to the component data.

Type Parameters

T

The component type.

Remarks

The global index is sparse across chunk boundaries; see Get<T>(int). Prefer GetReadonlyByEntity<T>(Entity) for entity-addressed access.

GetSpan<T>()

Gets a span of components for efficient iteration. Note: With chunked storage, this only returns the first chunk's span. For full iteration, use chunk-based methods.

[Obsolete("Use GetChunkSpan for chunked archetypes. This method only returns the first chunk. Will be removed in v1.0.")]
public Span<T> GetSpan<T>() where T : struct, IComponent

Returns

Span<T>

A span over component values in the first chunk.

Type Parameters

T

The component type.

Get<T>(int)

Gets a reference to a component for the entity at the specified global index.

public ref T Get<T>(int globalIndex) where T : struct, IComponent

Parameters

globalIndex int

The global index of the entity within this archetype.

Returns

T

A reference to the component data.

Type Parameters

T

The component type.

Remarks

The global index is sparse across chunk boundaries once a non-last chunk has a hole. Prefer GetByEntity<T>(Entity) for entity-addressed access; this overload is intended for chunk-local test and diagnostic use where the index is known to be dense.

Has(Type)

Checks if this archetype contains a specific component type.

public bool Has(Type type)

Parameters

type Type

The component type to check for.

Returns

bool

true if the archetype contains the component type; otherwise, false.

Has<T>()

Checks if this archetype contains a specific component type.

public bool Has<T>() where T : struct, IComponent

Returns

bool

true if the archetype contains the component type; otherwise, false.

Type Parameters

T

The component type to check for.

Set<T>(int, in T)

Sets a component value at the specified global index.

public void Set<T>(int globalIndex, in T component) where T : struct, IComponent

Parameters

globalIndex int

The global index of the entity within this archetype.

component T

The component value to set.

Type Parameters

T

The component type.

Remarks

The global index is sparse across chunk boundaries; see Get<T>(int). Prefer SetByEntity<T>(Entity, in T) for entity-addressed access.

ToString()

Returns a string that represents the current object.

public override string ToString()

Returns

string

A string that represents the current object.