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
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 a player presses a key on the client side Parameters:player: RpgPlayer- The player instancedata: { input: string, moving: boolean }- Input data
onLevelUp
Description: Called when a player increases one level Parameters:player: RpgPlayer- The player instancenbLevel: number- Number of levels gained
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: RpgClassMap<RpgMap>- The map class the player wants to enter
boolean | Promise<boolean>- Whether the player can change maps