Events
Introduction
Events are how entities can communicate with each other through a flexible listener and dispatch system.Event Listeners
Listener
Property | Type | Description |
---|---|---|
target | eid | Entity the event was dispatched on. |
currentTarget | eid | Entity the event was listened on. |
name | string | Name of the event. |
data | any | Event custom data |
Creating Event Listeners
addListener
world.events.addListener(target, name, listener)
Parameters
tip
It's possible to create global event listeners by using world.events.globalId
as the target.
Property | Type | Description |
---|---|---|
target | eid | Reference to the target entity. |
name | string | Name of the event to listen for. |
listener | Listener | The callback function for when an event is triggered |
Creating Event Handlers
When adding event listeners to entities, it’s crucial to set up handlers correctly to ensure they function as intended, especially when components are added to multiple entities. Improper handler creation can lead to stale references and unexpected behavior.
Suppose you’re creating a handler for an NPC entity that listens for a damaged event. The handler should update some schema and data values when the event occurs.