Entities · class

EntityAllocator

Slots, their generations, and a queue of the ones that are free.

Explained in Entities.

class EntityAllocator
import { EntityAllocator } from '@driftengine/entities';

In depth

The free list is first-in-first-out, and that is what makes the generation ceiling a number about the pool rather than about one slot. A stack hands the same slot straight back, so one spawn-and-destroy pair in a loop burns that slot's whole range while every other slot sits unused. A queue spreads the wear.

The cost is that a freed slot is not reused until every other freed slot has been, so a workload that churns a handful of entities touches more of the arrays than a stack would. That is a cache argument against a correctness one, and correctness wins.

Constructor

new

constructor(options?: EntityAllocatorOptions)
ParameterTypeDescription
options?EntityAllocatorOptions

Accessors

NameTypeDescription
liveCountgetnumber

Methods

create

create(): Entity

destroy

destroy(entity: Entity): boolean

Free an entity's slot. false when the handle was already stale, which is not an error.

ParameterTypeDescription
entityEntity
More

A caller destroying something twice is a caller that lost track, and the honest answer is that there was nothing to destroy — the same shape patchModule and Scope.leave use.

alive

alive(entity: Entity): boolean
ParameterTypeDescription
entityEntity

saveInto

saveInto(slot: AllocatorSnapshot): void

Copy this allocator's position into a slot, growing the slot's arrays if it has not held one this large before.

ParameterTypeDescription
slotAllocatorSnapshot
More

highWater bounds every copy, because slots beyond it have never been handed out and their generations are zero on both sides. That is what keeps the cost proportional to the world rather than to the arrays' capacity.

loadFrom

loadFrom(slot: AllocatorSnapshot): void

Put this allocator back to a saved position.

ParameterTypeDescription
slotAllocatorSnapshot
More

Slots between the saved highWater and the current one are cleared rather than left, and that is the whole difference between a restore and a partial one. An entity created after the save has a live slot the save knows nothing about; leaving it live would leave an entity in the world that the snapshot says does not exist, and its handle would keep answering alive.