# RpgPlayer

An RpgPlayer instance is created each time a player is connected. In an event, you often find a player parameter of type RpgPlayer. You have a series of methods to apply to the player

import { RpgPlayer, RpgPlayerHooks } from '@rpgjs/server'

export const player: RpgPlayerHooks = {
    onConnected(player: RpgPlayer) {
        // Making instructions when the player is connected
    }
}

Then put the class in the RpgServer decorator.


# props

  • Since: 3.0.0-beta.9
  • Property: props
  • Type: object
  • Optional: true
  • Usage:

Set custom properties on the player. Several interests:

  1. The property is shared with the client
  2. If you save with player.save(), the property will be saved to be reloaded later
  3. If you use horizontal scaling, the property will be kept in memory if the player changes the map and this map is on another server

Example:

import { RpgPlayerHooks } from '@rpgjs/server'

declare module '@rpgjs/server' {
 export interface RpgPlayer {
     nbWood: number
 }
}

export const player: RpgPlayerHooks = {
 props: {
     nbWood: Number
 }
}

This is a simple example. Let's say that the player can have a number of harvested woods, then

  1. you must specify the type for Typescript
  2. Add the property in props

You can also set up with this object:

 {
     $default: <any> (undefined by default), 
     $syncWithClient: <boolean> (true by default),
     $permanent: <boolean> (true by default)
 }
  • Indicate if the property should be shared with the client

Example:

export const player: RpgPlayerHooks = {
 props: {
     secretProp: {
         $syncWithClient: false
     }
 }
}
  • Indicate if the property should be registered in a database. If the data is just temporary to use on the current map:
export const player: RpgPlayerHooks = {
 props: {
     tmpProp: {
         $permanent: false
     }
 }
}

# onJoinMap

  • Property: onJoinMap
  • Type: (player: RpgPlayer, map: RpgMap) => any
  • Optional: true
  • Usage:

When the player joins the map


# onConnected

  • Property: onConnected
  • Type: (player: RpgPlayer) => any
  • Optional: true
  • Usage:

When the player is connected to the server


# onInput

  • Property: onInput
  • Type: (player: RpgPlayer, data: { input: Direction | Control | string, moving: boolean }) => any
  • Optional: true
  • Usage:

When the player presses a key on the client side


# onLeaveMap

  • Property: onLeaveMap
  • Type: (player: RpgPlayer, map: RpgMap) => any
  • Optional: true
  • Usage:

When the player leaves the map


# onLevelUp

WARNING

Stability: 1 - Experimental This feature is subject to change, and is gated by a command line flag. It may change or be removed in future versions.

  • Property: onLevelUp
  • Type: (player: RpgPlayer, nbLevel: number) => any
  • Optional: true
  • Usage:

When the player increases one level


# onDead

WARNING

Stability: 1 - Experimental This feature is subject to change, and is gated by a command line flag. It may change or be removed in future versions.

  • Property: onDead
  • Type: (player: RpgPlayer) => any
  • Optional: true
  • Usage:

When the player's HP drops to 0


# onDisconnected

  • Property: onDisconnected
  • Type: (player: RpgPlayer) => any
  • Optional: true
  • Usage:

When the player leaves the server


# onInShape

  • Property: onInShape
  • Type: (player: RpgPlayer, shape: RpgShape) => any
  • Optional: true
  • Usage:

When the player enters the shape


# onOutShape

  • Property: onOutShape
  • Type: (player: RpgPlayer, shape: RpgShape) => any
  • Optional: true
  • Usage:

When the player leaves the shape


# onMove

  • Since: 3.0.0-beta.4
  • Property: onMove
  • Type: (player: RpgPlayer) => any
  • Optional: true
  • Usage:

When the x, y positions change


# canChangeMap

  • Since: 3.0.0-beta.8
  • Property: canChangeMap
  • Type: (player: RpgPlayer, nextMap: RpgClassMap< RpgMap>) => boolean | Promise
  • Optional: true
  • Usage:

Allow or not the player to switch maps. nexMap parameter is the retrieved RpgMap class and not the instance