Table of Contents

Class ArchetypeManager

Namespace
KeenEyes
Assembly
KeenEyes.Core.dll

Manages archetype lifecycle and entity-to-archetype mappings. Handles archetype creation, entity migration, and query matching.

public sealed class ArchetypeManager : IDisposable
Inheritance
ArchetypeManager
Implements
Inherited Members

Remarks

The ArchetypeManager is the central coordinator for the archetype-based storage system. It maintains a mapping from ArchetypeId to Archetype instances and tracks which archetype each entity belongs to.

When an entity's component set changes, the ArchetypeManager handles the migration to a new archetype, copying shared components and updating all bookkeeping.

Constructors

ArchetypeManager(ComponentRegistry, ChunkPool?)

Manages archetype lifecycle and entity-to-archetype mappings. Handles archetype creation, entity migration, and query matching.

public ArchetypeManager(ComponentRegistry componentRegistry, ChunkPool? chunkPool = null)

Parameters

componentRegistry ComponentRegistry

The component registry for type information.

chunkPool ChunkPool

Optional chunk pool for chunk reuse. If null, a new pool is created.

Remarks

The ArchetypeManager is the central coordinator for the archetype-based storage system. It maintains a mapping from ArchetypeId to Archetype instances and tracks which archetype each entity belongs to.

When an entity's component set changes, the ArchetypeManager handles the migration to a new archetype, copying shared components and updating all bookkeeping.

Properties

ArchetypeCount

Gets the number of archetypes.

public int ArchetypeCount { get; }

Property Value

int

Archetypes

Gets all archetypes in this manager.

public IReadOnlyList<Archetype> Archetypes { get; }

Property Value

IReadOnlyList<Archetype>

ChunkPool

Gets the chunk pool used by this manager.

public ChunkPool ChunkPool { get; }

Property Value

ChunkPool

EntityCount

Gets the number of entities tracked by this manager.

public int EntityCount { get; }

Property Value

int

Methods

Clear()

Clears all archetypes and entity locations, returning all chunks to the pool.

public void Clear()

Remarks

This method resets the archetype manager to its initial state while keeping the chunk pool for reuse. This is useful for scenarios like restoring from a snapshot where the world state needs to be cleared before recreation.

Unlike Dispose(), this method leaves the manager in a usable state.

Dispose()

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

public void Dispose()

GetArchetype(ArchetypeId)

Gets an archetype by its ID, or null if it doesn't exist.

public Archetype? GetArchetype(ArchetypeId id)

Parameters

id ArchetypeId

The archetype identifier.

Returns

Archetype

The archetype, or null.

GetMatchingArchetypes(QueryDescription)

Gets all archetypes that match the specified query.

public IEnumerable<Archetype> GetMatchingArchetypes(QueryDescription description)

Parameters

description QueryDescription

The query description.

Returns

IEnumerable<Archetype>

Matching archetypes.

GetOrCreateArchetype(ArchetypeId)

Gets or creates an archetype for the specified archetype ID.

public Archetype GetOrCreateArchetype(ArchetypeId id)

Parameters

id ArchetypeId

The archetype identifier.

Returns

Archetype

The archetype for that ID.

GetOrCreateArchetype(IEnumerable<Type>)

Gets or creates an archetype for the specified component types.

public Archetype GetOrCreateArchetype(IEnumerable<Type> componentTypes)

Parameters

componentTypes IEnumerable<Type>

The component types.

Returns

Archetype

The archetype for those component types.

PreallocateArchetype(IEnumerable<Type>, int)

Pre-allocates an archetype for the specified component types with an initial capacity.

public Archetype PreallocateArchetype(IEnumerable<Type> componentTypes, int initialCapacity = 16)

Parameters

componentTypes IEnumerable<Type>

The component types for this archetype.

initialCapacity int

Initial capacity for entity storage. Defaults to 16.

Returns

Archetype

The pre-allocated archetype.

Remarks

Pre-allocating archetypes is useful for reducing the number of archetype transitions when spawning many entities with the same component set. This is particularly effective when used with bundles.

If an archetype with these component types already exists, this method returns the existing archetype without creating a new one.

Events

ArchetypeCreated

Event raised when a new archetype is created. Used for cache invalidation in the query system.

public event Action<Archetype>? ArchetypeCreated

Event Type

Action<Archetype>