Skip to main content

Client Sprite Hooks

Sprite hooks allow you to customize the behavior of sprites (players and events) on the client side. These hooks are defined in the sprite property of your client module.

Usage

Available Hooks

onInit

Description: Called when a sprite is initialized on the client Parameters:
  • sprite: RpgSprite - The sprite instance
Example:

onDestroy

Description: Called when a sprite is removed from the scene Parameters:
  • sprite: RpgSprite - The sprite instance
Example:

onBeforeRemove

Description: Called when the server requests sprite removal, before the sprite disappears from the scene. Return a promise to keep the sprite visible while a transition runs. The server does not decide how the transition is rendered. It only sends a removal context with event.remove(). The client reads that context in onBeforeRemove and can play an animation, sound, particle effect, GUI transition, or any combination of effects. Parameters:
  • sprite: RpgSprite - The sprite instance
  • context.reason?: string - Removal reason, such as "defeated"
  • context.data?: any - Custom data sent by event.remove()
  • context.transition?: object - Optional transition metadata. This is a project-defined payload; RPGJS forwards it to the client.
  • context.timeoutMs?: number - Safety timeout used by the server before the event is removed from the map.
Server example:
Client example:
sprite.setAnimation() returns a promise for finite animations. Awaiting it keeps the sprite visible until the animation finishes, is interrupted, or reaches the configured timeout. timeoutMs is the maximum time the server keeps the removed sprite visible on clients. Set it to at least the expected duration of the client transition. The client hook may finish earlier, but the event is removed from the map when the server timeout expires.

onChanges

Description: Called when sprite data changes (from server updates) Parameters:
  • sprite: RpgSprite - The sprite instance
  • data: any - New data
  • old: any - Previous data
Example:

onUpdate

Description: Called at each frame for sprite updates Parameters:
  • sprite: RpgSprite - The sprite instance
  • obj: any - Update data
Example:

onMove

Description: Called when the sprite’s position changes Parameters:
  • sprite: RpgSprite - The sprite instance
Example:

Complete Example