Class
EntityHandle
View sourceIdentifies one entity in a World with a reusable slot index and
generation.
The index selects an entity slot. The generation changes when the slot is
reused after despawn, so a handle identifies an entity only when both values
match. World checks both values before resolving a handle; a stale
handle therefore does not refer to a replacement entity.
EntityHandle#equals compares the two fields only and does not check
whether the handle is currently alive.
Use EntityHandle#id to pack the pair into an EntityId and
EntityHandle.from to unpack it. EntityHandle.serialize
returns the index and generation fields, while
EntityHandle.deserialize reconstructs a handle from a packed
numeric EntityId and can reuse an output handle.
Examples
import { EntityHandle, World } from '@wimaengine/ecs'
const world = new World()
const player = world.spawn([])
const copy = EntityHandle.from(player.id())
console.log(copy.equals(player)) // true
world.despawn(player)
const replacement = world.spawn([])
console.log(replacement.index === player.index) // true
console.log(replacement.generation !== player.generation) // true
console.log(world.get(player, EntityHandle)) // null: stale generation
const serial = EntityHandle.serialize(replacement)
const restored = EntityHandle.deserialize(replacement.id())
console.log(serial.index === restored.index) // trueConstructor
View source
Properties
readonlyindex: number
View sourceSlot index used to locate the entity in a world.
readonlygeneration: number
View sourceGeneration of the entity currently assigned to index.
A world increments this value when it reuses a despawned entity slot. Handles from an earlier generation are stale for that slot.
Methods
equals(other: EntityHandle): boolean
View sourceCompares this handle’s index and generation with another handle.
This is a value comparison only; it does not query a world to determine whether either handle still refers to a live entity.
id(): number
View sourcePacks the index and generation into a numeric entity identity.
The packed value preserves both parts of the handle and can be passed to
EntityHandle.from or EntityHandle.deserialize.
staticfrom(id: number): EntityHandle
View sourceCreates a handle from a packed numeric entity identity.
staticserialize(value: EntityHandle): EntityHandleSerial
View sourceConverts a handle to its plain serialized field representation.
staticdeserialize(value: number, out?: EntityHandle): EntityHandle
View sourceReconstructs a handle from a packed numeric entity identity.
If out is provided, its index and generation are updated in place and
the same object is returned. Otherwise, a new handle is created.
staticvalidateSerial(value: unknown): value is number
View sourceChecks whether a value is a numeric entity identity.
This is the validation accepted by EntityHandle.deserialize; it
checks the value’s type, not whether the identity is live in a particular
World.
