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. Script source that JS or Lua plugins require, in the library’s own language. Compiled plugins can add them too.
  • Services. Objects other plugins call, in any language.

Another plugin’s types are in its namespace, its id in PascalCase, so the freecam plugin’s are in Freecam. In your own .sal files they work by their short names, like your own do, as long as nothing closer has the same name. The full name, like Freecam.Rig, always works. See Namespaces.

Import the namespace from wraith, like import { Freecam } from "wraith", then they’re under it, like Freecam.Rig.at(address). Nothing’s a global.

A library is code other scripts load with require(name), and tweaks with @use name. libraries.add(name, language, source) adds one, and it’s there until your plugin goes. Libraries only work in their own language. Use a service to reach every language.

JS plugins load one with require(name) or import x from "name".

import { Wraith } from "wraith";
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.

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

schemas/freecam.sal
[Service] trait Camera {
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.

Your plugin gives an object for each [Service] trait in its own schemas. It fails to load if it marks a trait [Service] and doesn’t give one, so a typo shows up straight away.

Export an object with the trait’s name from main.ts:

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

A plugin that depends on freecam gets its service as a function in its namespace, named after the trait with a small first letter, so Freecam.Camera is Freecam.camera(). The trait’s instance() gives the same. Scripts import Freecam from wraith like their own namespace.

import { Wraith, Freecam } from "wraith";
const camera = Freecam.camera();
camera.setSpeed(2);
const pos = await camera.position();
camera.onMove({ moved(pos) { Wraith.log().info(`${pos.x}`); } });

Freecam.Camera.instance() is the same object.

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.

Your editor and build need the other plugin’s schemas. They’re found for each plugin you depend on in this order: the folder your devDepends names, your dev plugins, a folder by its name next to yours, the installed one, then the hub, which is downloaded into a cache. Only versions your depends allows count, except ones you’re working on. wraith sdk deps shows where each one came from.

For one you’re working on, point at it:

"depends": ["freecam"],
"devDepends": { "freecam": "../freecam" }

npm run types writes the typings, with each plugin you depend on in its own namespace.

A service is something others call. A slot is the other way round: somewhere others add to, like viewers for a kind of file, each an object of a trait in your schemas. ui.defineSlot makes one, plugin.id to others, and its contributions() are what’s in it, in the order added:

// schemas/viewer.sal in the `sheets` plugin
trait Viewer {
string name();
void draw(u32 row);
}
import { Wraith } from "wraith";
import { as } from "salamander";
const viewers = Wraith.ui().defineSlot({ id: "viewers", title: "Viewers", help: "Ways to show a row", trait: "Sheets.Viewer" });
for (const v of viewers.contributions()) as("Sheets.Viewer", v).draw(7);

A plugin depending on yours adds an object of the trait with ui.contributeTo. It has to be that trait, or it fails with WrongTrait. A script names the trait its object implements with implement, since anything can go where any object does; native code passes one as any trait object:

import { Wraith } from "wraith";
import { implement } from "salamander";
Wraith.ui().contributeTo("sheets.viewers", implement("Sheets.Viewer", {
name() { return "Chart"; },
draw(row) { /* ImGui calls */ },
}));

What a plugin adds goes with it. A slot goes with its plugin too, but what’s been added stays for it, so it’s all there again when that plugin reloads. ui.slots() lists plugins’ slots with Wraith’s own.

Wraith’s own slots take objects the same way, each of the trait its Slot.Info says: Wraith.PhotoPart for Photo mode’s, like a foldout in photo.inspector or a node in photo.scene, Wraith.SettingsSection for your page in settings, Wraith.HubButton for a tile on the overlay’s bar in hub, and Wraith.WorkbenchTab for workbench. Each trait’s describe says where it goes, like a part’s kind, so contributeTo is all it takes. In Zig, sdk.slots.addPhotoPart and addSettingsSection do it from a struct you keep and a function drawing it.

Services run on whatever thread called, so what they share needs guarding. To share a lock with other plugins, make one with sync.newLock() and hand it out through a service. Each plugin locks it through its own sync, and waiting never blocks, since sync.lock gives a promise.

import { Wraith } from "wraith";
const sync = Wraith.sync();
const lock = sync.newLock();
await sync.lock(lock);
try {
// yours
} finally {
sync.unlock(lock);
}

When it’s let go of, whoever waited longest gets it next. What a plugin holds or waits for goes with it when it unloads, and the lock goes with the plugin that made it.

Made with ♥ by Asgard