Skip to content

Sharing between plugins

Plugins can lean on each other, like one that knows an engine and others that use it. List the ones you need in depends in your manifest so they load first.

There are three ways to share, from simplest:

  • Schemas. A plugin that depends on another gets its schemas too, so it can use the same game types without copying them.
  • Libraries. Source code other scripts require, in the same language.
  • Services. Objects other plugins call, in any language.

A library is code other scripts load with require(name) or import x from "name", and tweaks with @use name. It’s there until your plugin goes.

import { libraries } from "wraith";
libraries.add("hello", "js", "module.exports = (name) => `hi ${name}`;");

A JS library is CommonJS style. Whatever it puts in module.exports is what require gives.

Libraries only work in their own language. Use a service to reach every language.

A service is a trait in your schemas marked [Service]. It says what the other plugins can call:

schemas/freecam.sal
struct Vec3 { f32 x; f32 y; f32 z; }
[Service] trait Freecam {
void setSpeed(f32 speed);
async Vec3 position();
void onMove(keep Mover mover);
}
trait Mover { void moved(Vec3 pos); }

Plugins that depend on yours get these types too, so both sides agree on them. Args and results are what the trait says, like numbers, strings, structs and other traits. A method marked async gives a promise.

Offer it by exporting an object with the trait’s name from main.js:

import type { Freecam as FreecamService } from "schema";
let speed = 1;
const movers = [];
export const Freecam: FreecamService = {
setSpeed(s) {
speed = s;
},
async position() {
return { x: 1, y: 2, z: 3 };
},
onMove(mover) {
movers.push(mover);
},
};

Use it by importing the plugin by name:

import { Freecam } from "freecam";
Freecam.setSpeed(2);
const pos = await Freecam.position();
Freecam.onMove({ moved(pos) { console.log(pos.x); } });

wraith sdk js types types it for you, from the schemas of the plugins you depend on. It finds them next to your plugin’s folder, or installed.

Your plugin fails to load if it marks a trait [Service] and doesn’t give one, so a typo shows up straight away.

A plugin that depends on yours unloads before yours does, so its services never go away under it. Methods run on whatever thread called, so keep them quick.

Made with ♥ by Asgard