Lifecycle methods
ecs.registerComponent({
name: 'my-component',
add: (world, component) => {
}
remove: (world, component) => {
}
tick: (world, component) => {
}
})
Overview
Method | Description |
---|---|
add | Called once when the component is initialized. Used to set up initial state and instantiate variables |
remove | Called 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. |
tick | Called on each render loop or tick of the scene. Used for continuous changes or checks. |
Parameters
Property | Type | Description |
---|---|---|
world | world | See the world API Reference. |
component | ComponentObject | The component handle. |
ComponentObject
The component handle is the second argument to callbacks such as add
/remove
/tick
. It contains the following fields:
Property | Type | Description |
---|---|---|
eid | Eid | The current entity ID |
schema | Cursor | Points to the current entity’s schema |
schemaAttribute | World-scoped component | The current component |
data | Cursor | Points to the current entity’s data (if defined) |
dataAttribute | World-scoped component | The 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.