Skip to main content

Creating Modules in RPGJS

This guide explains how to create and structure modules in RPGJS using defineModule and createModule.

Module Structure

A typical module consists of three main files:
  • server.ts - Server-side logic and hooks
  • client.ts - Client-side logic and hooks
  • index.ts - Module configuration and dependency injection

Step 1: Define Server-Side Module

Create a server.ts file using defineModule to define server-side behavior:

Step 2: Define Client-Side Module

Create a client.ts file using defineModule for client-side logic:

Step 3: Create Module with Dependency Injection

Create an index.ts file using createModule to configure the module and its dependencies:

Understanding createModule Parameters

The createModule function takes two parameters:

1. Token Name (First Parameter)

  • Purpose: Unique identifier for dependency injection
  • Usage: Allows other modules to inject this module as a dependency
  • Example: "Battle" can be injected elsewhere using this token

2. Dependencies Array (Second Parameter)

The array can contain two types of objects:

Dependency Injection Objects

Server/Client Extension Objects

How Hooks Work

When you include server and client objects in the createModule array, they extend the core modules with additional hooks:
  • Server hooks: Extend RpgServer with custom player and event behaviors
  • Client hooks: Extend RpgClient with custom client-side logic
  • Event-driven: Hooks are automatically called at specific game events

Example Usage

This modular approach allows for clean separation of concerns, easy testing, and flexible dependency management in your RPGJS game.