EntityCell

View source

Provides a view of one EntityHandle in a World.

A cell combines the requested handle with the EntityLocation selected by its index and generation. Use World#getEntity to create a cell, then use it to inspect the entity’s current component layout or read component values from its table row.

If the handle is missing, already despawned, or has a stale generation, the cell uses an invalid location. In that state EntityCell#exists returns false and EntityCell#components returns an empty list. Call EntityCell#exists before EntityCell#get or EntityCell#has; those methods access the cell’s table or archetype directly and are not guarded for an invalid location.

When a matching location exists, the cell keeps the world’s location record and observes its table, row, and archetype updates. A cell does not perform a fresh generation check on every method call, so discard and reacquire it after despawning an entity or reusing its storage slot.

Examples

import { World } from '@wimaengine/ecs'

class Position {
  constructor(x = 0) {
    this.x = x
  }
}

const world = new World()
const entity = world.spawn([new Position(1)])
const cell = world.getEntity(entity)

if (cell.exists() && cell.has([Position])) {
  const position = cell.get(Position)
  if (position) position.x += 1
}

world.despawn(entity)
console.log(world.getEntity(entity).exists()) // false

Constructor


constructor(world: World, entity: EntityHandle)

View source

Creates a cell view for an entity in a world.

The entity index and generation are checked against the world’s entity registry. If they do not identify a matching location, the cell is still created with an invalid location; use EntityCell#exists to test it before reading or checking components.

Parameters

NameDescriptionDefault
world——
entity——

Properties


location: EntityLocation

View source

Storage location for this cell’s entity.

A matching live handle uses the location record owned by the world. A missing or stale handle gets a new location whose identifiers are -1. The world updates this record when the entity changes tables or is despawned.

Methods


exists(): boolean

View source

Checks whether this cell currently points at a valid storage location.

The check succeeds when the location has a table id, row index, and archetype id other than -1. It does not perform a new lookup or compare the location’s generation with the handle, so reacquire a cell after its entity is despawned or its slot is reused.

Returns

true when the location can identify an entity row.

get<T>(type: Constructor<T>): T

View source

Reads a component by its constructor.

The constructor is converted to its TypeId and the component is read from this cell’s table row. A live entity that does not contain the requested component returns undefined. Call EntityCell#exists first; an invalid cell has no table and is not handled by this method.

Type Parameters

T

Parameters

NameDescriptionDefault
type——

Returns

The component value, or undefined when it is not present.

getTypeId(typeid: TypeId): unknown

View source

Reads a component by its raw TypeId.

This is the type-ID form of EntityCell#get. It returns the value stored in the cell’s table row, or undefined when the live entity has no column for typeid. Call EntityCell#exists before using it because an invalid cell has no table.

Parameters

NameDescriptionDefault
typeid——

Returns

The component value, or undefined when it is not present.

has<T>(types: Constructor<T>[]): boolean

View source

Checks whether the entity has every requested component type.

Constructors are converted to TypeId values and checked against the cell’s current archetype. An empty list matches any valid archetype. Call EntityCell#exists first; an invalid cell has no archetype.

Type Parameters

T

Parameters

NameDescriptionDefault
types——

Returns

true when every requested type is present.

hasTypeid(typeids: TypeId[]): boolean

View source

Checks whether the entity has every requested type ID.

This is the raw type-ID form of EntityCell#has. It checks the cell’s current archetype without reading component values. Call EntityCell#exists first; an invalid cell has no archetype.

Parameters

NameDescriptionDefault
typeids——

Returns

true when every requested type ID is present.

components(): readonly TypeId[]

View source

Lists the type IDs in the entity’s current archetype.

This returns component type IDs, not component values or constructors. A live entity spawned by World#spawn also includes EntityHandle as a component. If the cell has no archetype, including for a stale or missing entity, this returns an empty list.

Returns

The component type IDs, or [] without an archetype.

id(): EntityHandle

View source

Returns the handle associated with this cell.

The returned handle is the one supplied when the cell was created. This method does not check whether the handle is still live, so a stale cell returns its stale handle.

Returns

The cell’s entity handle.