PLUGINS & SCRIPTING

Creating a plugin

Write your own plugin: change the engine's behavior with inject, add parameters and commands, and share it.

The video tutorial for this page is coming later. Subscribe to the RPG Paper Maker YouTube channel to see it as soon as it is out.

A plugin is JavaScript code loaded when the game starts. It can change how the engine works, draw new things, and add its own commands. Read Scripting basics first.

We recommend Visual Studio Code to write plugins, with the engine source and the API documentation open next to it.

Create the plugin#

  1. Open the Plugins manager, double click an empty line, and choose Create empty.
  2. Give it a unique Name and a Category (Battle, Map, Menus or Others).
  3. Fill in its details with Edit: Author, Description, Version (like 1.0.0), Website, Tutorial (a link explaining how to use it), and a Picture.

doc80.png

RPM creates a folder with the plugin's name in your project's Plugins folder, with two files:

  • code.js: the plugin's code. Edit it in the Code tab, or in your code editor.
  • details.json: its details, parameters and commands (edited in the Plugins manager).

A new plugin starts with:

const pluginName = "MyFirstPlugin";
const inject = Manager.Plugins.inject;

// Start code here

The engine modules (Common, Core, Data, Model, Manager, Scene, Graphic, EventCommand) and THREE are available directly, without imports.

Try it: add console.log("Hello world!");, start a play test and open the console with Ctrl+Shift+I.

Change the engine with inject#

inject adds your code to an existing method of the engine, without copying it:

inject(Scene.TitleScreen, "load", async function () {
    console.log("Title screen loaded!");
}, false, false, true);

The arguments:

inject(
    Scene.TitleScreen, // The class to change
    "load",            // The method (or variable) name
    async function () { console.log("Title screen loaded!"); }, // Your code
    false,             // true for a static method or variable
    false,             // true to replace the original code entirely (overwrite)
    true               // true to run the original code before yours, false after
);

Inside your function:

  • this is the object, as in the original method.
  • this.callResult is what the original method returned (when it runs before yours), so you can change it.
  • this.super() calls the original method yourself, when you overwrite it and want to call it only sometimes.

Use function () {}, never arrow functions () => {}: arrow functions have no this of their own, so injecting them doesn't work.

Example: an icon on map#

const pluginName = "MapIcon";
const inject = Manager.Plugins.inject;

// Create the icon when a map loads
inject(Scene.Map, "load", async function () {
    await this.callResult; // wait for the original map loading
    this.pluginIcon = Data.Pictures.getPictureCopy(Common.PICTURE_KIND.ICONS, 1);
});

// Draw it over the map every frame
inject(Scene.Map, "drawHUD", function () {
    this.pluginIcon?.draw({ x: 20, y: 20 });
});

doc81.png

Static methods#

Some methods belong to the class itself (static). Pass true as the fourth argument:

inject(Common.Mathf, "random", function (min, max) {
    return min; // A not very random game...
}, true, true);

Parameters#

Parameters let people using your plugin set it up without touching the code. Add them in the plugin's Parameters list: a Name, a Description, and a Default value of any type (number, text, switch, variable, an icon, a hero, a color, a key…, a Custom structure or a Custom list).

doc82.png

Read a parameter with Manager.Plugins.getParameter(pluginName, "name"):

// Create the icon when a map loads
inject(Scene.Map, "load", async function () {
    const id = Manager.Plugins.getParameter(pluginName, "iconID");
    await this.callResult; // wait for the original map loading
    this.pluginIcon = Data.Pictures.getPictureCopy(Common.PICTURE_KIND.ICONS, id);
});
  • Custom structures come as objects (getParameter(pluginName, "size").x) and custom lists as arrays.
  • A parameter set to a variable gives the variable's current value.
  • Choices of game data (a hero, an item, a color…) give the matching data; console.log it to see what you get.

Commands#

A plugin can add its own commands, used with the Plugin command in reactions. Add a command in the plugin's Commands list (a Name, a Description and its Parameters), then register what it does:

Manager.Plugins.registerCommand(pluginName, "print", (text) => {
    console.log(text);
});

The command's parameter values are passed to your function, in order. In a reaction, add Plugin, choose your plugin and the print command, and type a text.

doc83.png doc84.png

Tips#

  • Keep your code in functions injected into the engine rather than running things at load: plugins are loaded before the game data.
  • Prefix what you add to engine objects (this.myPluginIcon) so you don't clash with the engine or other plugins.
  • Test with other popular plugins enabled.
  • Increase the Version each time you share an update, so Check update works for your users.

Share it#

  • Export it locally to send the plugin folder to someone (they use Import from local plugin).
  • Export to the online database to propose it for the online plugins list. Online plugins live in the official GitHub repository and appear on the Plugins page.