Creating Modules in RPGJS
This guide explains how to create and structure modules in RPGJS usingdefineModule and createModule.
Module Structure
A typical module consists of three main files:server.ts- Server-side logic and hooksclient.ts- Client-side logic and hooksindex.ts- Module configuration and dependency injection
Step 1: Define Server-Side Module
Create aserver.ts file using defineModule to define server-side behavior:
Step 2: Define Client-Side Module
Create aclient.ts file using defineModule for client-side logic:
Step 3: Create Module with Dependency Injection
Create anindex.ts file using createModule to configure the module and its dependencies:
Understanding createModule Parameters
ThecreateModule 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 includeserver and client objects in the createModule array, they extend the core modules with additional hooks:
- Server hooks: Extend
RpgServerwith custom player and event behaviors - Client hooks: Extend
RpgClientwith custom client-side logic - Event-driven: Hooks are automatically called at specific game events