Subscribe to Changes
Detect changes in a component and run functions on every change
import { AvatarEquippedData } from '@dcl/sdk/ecs'
export function main() {
AvatarEquippedData.onChange(engine.PlayerEntity, (equipped) => {
if (!equipped) return
console.log('New wearables list: ', equipped.wearableUrns)
console.log('New emotes list : ', equipped.emoteUrns)
})
}// define component
export const MyComponent = engine.defineComponent('myComponent', {
value1: Schemas.Boolean,
value2: Schemas.Float,
})
// Usage
export function main() {
// Create entities
const myEntity = engine.addEntity()
// Create instances of the component
MyComponent.create(myEntity, {
value1: true,
value2: 10,
})
// Subscribe to changes
MyComponent.onChange(myEntity, (componentData) => {
if (!componentData) return
console.log(componentData.value1)
console.log(componentData.value2)
})
}Last updated