ComponentHooks

View source

Configures optional synchronous callbacks for one component type.

Register an instance with World#setComponentHooks for one world, or stage it with App#setComponentHooks before App#run to apply the same configuration to the app’s worlds. Callbacks run synchronously during entity component mutations; each callback’s exact timing is described on its field.

Examples

import { ComponentHooks, World } from '@wimaengine/ecs'

class Player {}
const world = new World()

world.setComponentHooks(Player, new ComponentHooks(
  (entity, world) => console.log('Player added', entity, world),
  (entity, world) => console.log('Player removed', entity, world)
))

const player = world.spawn([new Player()])
world.despawn(player)

Constructor


constructor(add?: ComponentHook, remove?: ComponentHook, insert?: ComponentHook)

View source

Creates a lifecycle-hook configuration.

The arguments set the add, remove, and insert callbacks in that order. Omitted or explicitly undefined callbacks become null.

Parameters

NameDescriptionDefault
add?

Callback after component storage.

null
remove?

Callback before component storage is removed.

null
insert?

Callback after the resulting component set is stored.

null

Properties


add: ComponentHook

View source

Runs after this component’s value is stored on an entity. World#spawn invokes it for each component type in the new entity. World#insert invokes it once for each supplied component type, including a type that was already present. The callback receives the affected EntityHandle and owning World.

remove: ComponentHook

View source

Runs before this component’s storage is removed. The entity still has its row when the callback runs. World#remove invokes it once for each requested type, even when that type is absent. World#despawn invokes it once for each component type on the entity before the entity is deleted. The callback receives the affected EntityHandle and owning World.