Skip to main content

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 the player 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 as maxHp, 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
Typical usage:
  • 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
If your player data comes from your own database, a save slot, or a snapshot, do not call 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
Example:

onJoinMap

Description: Called when a player joins a map Parameters:
  • player: RpgPlayer - The player instance
  • map: RpgMap - The map instance the player joined
Example:

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 a data.id equal to 'start'. This is typically used after a title screen or another start menu. Parameters:
  • player: RpgPlayer - The player instance
Example:

onLeaveMap

Description: Called when a player leaves a map Parameters:
  • player: RpgPlayer - The player instance
  • map: RpgMap - The map instance the player left
Example:

onInput

Description: Called when a player presses a key on the client side Parameters:
  • player: RpgPlayer - The player instance
  • data: { input: string, moving: boolean } - Input data
Example:

onLevelUp

Description: Called when a player increases one level Parameters:
  • player: RpgPlayer - The player instance
  • nbLevel: number - Number of levels gained
Example:

onSkillChange

Description: Called after a player learns or forgets a skill. Parameters:
  • player: RpgPlayer - The player instance
  • payload.action: 'learn' | 'forget' - Skill change type
  • payload.skill - Skill data
  • payload.skillId: string - Skill identifier
  • payload.source?: string - Change source, such as manual, level, or studio
  • payload.level?: number - Level that triggered the change, when available
Example:

onDead

Description: Called when a player’s HP drops to 0 Parameters:
  • player: RpgPlayer - The player instance
Example:

onDisconnected

Description: Called when a player leaves the server Parameters:
  • player: RpgPlayer - The player instance
Example:

onMove

Description: Called when the player’s x, y positions change Parameters:
  • player: RpgPlayer - The player instance
Example:

onInShape / onOutShape

Description: Called when a player enters or leaves a shape Parameters:
  • player: RpgPlayer - The player instance
  • shape: RpgShape - The shape instance
Example:

canChangeMap

Description: Determines if a player can change to a specific map Parameters:
  • player: RpgPlayer - The player instance
  • nextMap: RpgClassMap<RpgMap> - The map class the player wants to enter
Returns:
  • boolean | Promise<boolean> - Whether the player can change maps
Example:

Complete Example