Zum Hauptinhalt springen

Lifecycle methods

ecs.registerComponent({
name: 'my-component',
add: (world, component) => {
}
remove: (world, component) => {
}
tick: (world, component) => {
}
})

Overview

MethodDescription
addCalled once when the component is initialized. Used to set up initial state and instantiate variables
removeCalled when the component is removed from the entity or when the entity is detached from the scene. Used to undo all previous modifications to the entity.
tickCalled on each render loop or tick of the scene. Used for continuous changes or checks.

Parameters

PropertyTypeDescription
worldworldSee the world API Reference.
componentComponentObjectThe component handle.

ComponentObject

The component handle is the second argument to callbacks such as add/remove/tick. It contains the following fields:

PropertyTypeDescription
eidEidThe current entity ID
schemaCursorPoints to the current entity’s schema
schemaAttributeWorld-scoped componentThe current component
dataCursorPoints to the current entity’s data (if defined)
dataAttributeWorld-scoped componentThe component reference of the data (if defined)

This allows callbacks to load and set data on the entity even in callbacks.

info

The component handle object is reused and updated in place when being applied to multiple entities. The eid, schema, and data properties can only be used synchronously within the add/tick/remove and should not be accessed at any other time such as within timers or event handlers.