Skip to content
On this page

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:

ts
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:

```ts
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:
ts
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

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

When the player increases one level


onDead

  • 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