Skip to main content

Component Lifecycle

Introduction​

Lifecyle events can be used to trigger Component behavior on add, tick, or remove.

Example​

const MyComponent = ecs.registerComponent({
name: 'My Component',
add: (world, component) => {
console.log('My component was added to', component.eid)
},
tick: (world, component) => {
console.log('My tick function is running on', component.eid)
},
remove: (world, component) => {
console.log('My component was removed from', component.eid)
},
})

Methods​

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
worldWorldReference to the World.
componentComponentObjectReference to the current Component.

ComponentObject​

warning

Use schemaAttribute or dataAttribute instead of eid, schema, or data properties in asynchronous contexts like timers or event handlers.

PropertyTypeDescription
eideidThe Entity ID of the current Component
schemaCursorReference to the current Entity's schema
schemaAttributeComponentObjectReference to the current Component's schema in World Scope.
dataCursorReference to the current Entity's data
dataAttributeComponentObjectReference to the current Component's data in World Scope.