EntityHandle

View source

Identifies 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) // true

Constructor


constructor(index: number, generation: number)

View source

Parameters

NameDescriptionDefault
index

Entity slot index.

—
generation

Generation associated with the slot.

—

Properties


readonlygeneration: number

View source

Generation 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 source

Compares 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.

Parameters

NameDescriptionDefault
other

Handle to compare.

—

Returns

Whether both handles identify the same generation.

staticfrom(id: number): EntityHandle

View source

Creates a handle from a packed numeric entity identity.

Parameters

NameDescriptionDefault
id

Packed index and generation.

—

Returns

Handle reconstructed from id.

staticserialize(value: EntityHandle): EntityHandleSerial

View source

Converts a handle to its plain serialized field representation.

Parameters

NameDescriptionDefault
value

Handle to serialize.

—

Returns

Object containing the index and generation.

staticdeserialize(value: number, out?: EntityHandle): EntityHandle

View source

Reconstructs 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.

Parameters

NameDescriptionDefault
value

Packed index and generation.

—
out?

Optional handle to populate.

...

Returns

The populated or newly created handle.

staticvalidateSerial(value: unknown): value is number

View source

Checks 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.

Parameters

NameDescriptionDefault
value

Value to validate.

—

Returns

Whether value is a number.