Create an event
An event is any interactive object on the map: an NPC, a chest, a switch, an enemy, or a trigger.Create a simple event
You can define an event as an object with hooks:Attach the event to a map
Register it in your server module:EventDefinition only describes the event behavior. Map placement fields such as id, x, and y belong to the outer wrapper in maps[].events.
Inside object-based hooks, this is typed as RpgEvent, so methods like this.setGraphic() are inferred correctly by TypeScript.
Event hooks
The most common hooks are:onInit()for base event setup when the instance is createdonChanges(player)for reactive updates based on player stateonAction(player)when the player interacts with the eventonPlayerTouch(player)when the player touches the event
onInShape(zone, player)onOutShape(zone, player)onDetectInShape(player, shape)onDetectOutShape(player, shape)
onInit() vs onChanges(player)
The most important distinction is onChanges(player).
onInit() runs when the event instance is created. Use it for default data that is not tied to a specific player interaction yet: a base graphic, speed, direction, or movement route.
onChanges(player) runs during the change-detection cycle for that player. When player state changes, especially player variables, RPGJS re-runs this cycle and the event can react to it. You can also trigger it manually with player.syncChanges().
This means that some code can legitimately appear in both hooks:
onInit()gives the event a correct initial stateonChanges(player)keeps that state in sync when the player data changes
onInit() can set an initial graphic so the chest is immediately visible in the right state. Then onChanges(player) can recompute the same condition from a player variable and switch the graphic to opened or closed whenever that variable changes.
Use player variables for this kind of per-player state. They are persisted with the player and are available again after a save, a reconnect, or a map change.
Shared or scenario
When building an MMORPG, choose the right mode:- use shared for global world state
- use scenario for per-player progression