Server Event Hooks
Event hooks allow you to customize the behavior of events on the server side. These hooks are defined in theevent property of your server module and apply to all events in your game.
Usage
Available Hooks
onInit
Description: Called when an event is initialized on the map. UseonInit for base event setup that does not depend on a change-detection cycle yet: initial graphic, movement route, speed, direction, attached shapes, or default metadata.
Parameters:
event: RpgEvent- The event instance
onAction
Description: Called when a player interacts with an event (typically by pressing the action key) Parameters:event: RpgEvent- The event instanceplayer: RpgPlayer- The player who interacted with the event
onBeforeCreated
Description: Called before an event is created, allowing you to modify the event object or return a custom event configuration Parameters:object: any- The raw event data from the map, typically{ id?, x?, y?, event }for positioned eventsmap: RpgMap- The map instance where the event will be created
any- Modified event configuration
onPlayerTouch
Description: Called when a player touches an event (collision detection) Parameters:event: RpgEvent- The event instanceplayer: RpgPlayer- The player who touched the event
onDetectInShape / onDetectOutShape
Description: Called when a player enters or leaves an event’s detection shape Parameters:event: RpgEvent- The event instanceplayer: RpgPlayer- The player who entered/left the shapeshape: RpgShape- The shape instance
onInShape / onOutShape
Description: Called when the event itself enters or leaves a shape Parameters:event: RpgEvent- The event instanceshape: RpgShape- The shape instance
onChanges
Description: Called during the change-detection cycle for a player. This hook is the reactive part of event design. When player state changes, especially player variables, RPGJS can re-run the cycle and callonChanges(event, player) so the event can recompute its state for that player. You can also force that cycle with player.syncChanges().
Parameters:
event: RpgEvent- The event instanceplayer: RpgPlayer- The player who caused the change (if applicable)
Designing onInit and onChanges together
In practice, these two hooks often share similar logic:
onInitsets a correct initial state when the event appearsonChangeskeeps that state synchronized with player data later