Server Player Hooks
Player hooks allow you to listen to player-specific events and customize player behavior on the server side. These hooks are defined in theplayer property of your server module.
Usage
Custom Properties
You can define custom properties that will be synchronized with the client and optionally saved to the database:Available Hooks
Initializing Default Stats
RPGJS can provide built-in default parameters for a player such asmaxHp, maxSp,
str, int, dex, and agi.
Use player.initializeDefaultStats() when you want to:
- apply the built-in default parameter curves
- initialize HP/SP from those max values
- ensure the client receives visible HP/SP and parameter values on first game load
- call it in
onConnected()if your game starts immediately - call it in
onStart()if your game begins after a title screen or another GUI flow
player.initializeDefaultStats() after hydration unless you intentionally want to
overwrite those values. In that case, restore your data first and let that state be
synchronized to the client.
If you only want the built-in parameter curves without restoring HP/SP, use
player.applyDefaultParameters().
onConnected
Description: Called when a player connects to the server Parameters:player: RpgPlayer- The player instance
onLoad
Description: Called after a JSON or slot snapshot has been applied to the player. Parameters:player: RpgPlayer- The hydrated player instancesnapshot: RpgPlayerSnapshot- The resolved snapshot applied to the player
onSave
Description: Called after the snapshot is created and before a slot is persisted. Calling the legacyplayer.save() overload without a slot only serializes the snapshot
and does not emit this hook.
onJoinMap
Description: Called when a player joins a map Parameters:player: RpgPlayer- The player instancemap: RpgMap- The map instance the player joined
onStart
Description: Called when the player starts the game from a GUI interaction This hook is executed after a GUI interaction when the GUI returns adata.id equal to 'start'.
This is typically used after a title screen or another start menu.
Parameters:
player: RpgPlayer- The player instance
onLeaveMap
Description: Called when a player leaves a map Parameters:player: RpgPlayer- The player instancemap: RpgMap- The map instance the player left
onInput
Description: Called when the server receives an action from the client. Parameters:player: RpgPlayer- The player instanceinput: RpgActionInput<unknown>- Action name and optional custom data
onLevelUp
Description: Called when a player increases one level Parameters:player: RpgPlayer- The player instancenbLevel: number- Number of levels gained
onSkillChange
Description: Called after a player learns or forgets a skill. Parameters:player: RpgPlayer- The player instancepayload.action: 'learn' | 'forget'- Skill change typepayload.skill- Skill datapayload.skillId: string- Skill identifierpayload.source?: string- Change source, such asmanual,level, orstudiopayload.level?: number- Level that triggered the change, when available
onDead
Description: Called when a playerโs HP drops to 0 Parameters:player: RpgPlayer- The player instance
onDisconnected
Description: Called when a player leaves the server Parameters:player: RpgPlayer- The player instance
onMove
Description: Called when the playerโs x, y positions change Parameters:player: RpgPlayer- The player instance
onInShape / onOutShape
Description: Called when a player enters or leaves a shape Parameters:player: RpgPlayer- The player instanceshape: RpgShape- The shape instance
canChangeMap
Description: Determines if a player can change to a specific map Parameters:player: RpgPlayer- The player instancenextMap: RpgMapChangeTarget- A descriptor containing the destination map ID
boolean | Promise<boolean>- Whether the player can change maps