Skip to main content

Items System

The items system in RPGJS allows you to manage player inventory, equipment, and item usage. You can add items to players in three different ways: using string IDs, item classes, or item objects. Studio regular items can also select a spritesheet animation, a personal sound, and/or a built-in particle preset for successful use. This feedback runs from the authoritative native onUse hook and can be combined with an item workflow.

Adding Items

Method 1: String ID (Pre-registered Items)

Items must be registered in the database property of provideServerModules before they can be used with string IDs.
Then you can use it:
Note: The @RpgModule decorator is available for backward compatibility with v4, but the modern approach is to use provideServerModules with the database property directly.

Method 2: Item Class (Auto-registered)

When using an item class directly, it will be automatically added to the map’s database if not already present.

Method 3: Item Object (Dynamic Items)

You can create items dynamically using objects. These are automatically added to the map’s database.
If you don’t provide an id, one will be auto-generated:

Dynamic Item Creation in Events

When creating items dynamically (especially in events), you should first add them to the map’s database, then add them to the player.

Example: Event Giving an Item

Using String ID After Dynamic Registration

If you want to use a string ID after dynamically adding an item:

Item Hooks

Items can implement lifecycle hooks that are called at specific moments:
  • onAdd(player): Called when the item is added to inventory
  • onUse(player): Called when the item is successfully used
  • onUseFailed(player): Called when item usage fails (e.g., chance roll)
  • onRemove(player): Called when the item is removed from inventory
  • onEquip(player, equip): Called when the item is equipped/unequipped
Use onUse and onUseFailed for consumable items. Weapons and armors use onEquip instead and should not declare consumable-use hooks. When an item is used by string ID, RPGJS resolves object-based lifecycle hooks from the current map database. Updating that database entry therefore replaces the hook used by an item that is already present in the player’s inventory. Class-based items keep using their instantiated class hooks.

Using Classes

Using Objects

Managing Database

Adding Items to Database

Removing Items from Database

Complete Example: Shop Event