Skip to content

Plugin structure

Here’s what the template gives you.

my-plugin/
src/main.ts your code
schemas/game.sal the game's types
public/ copied into dist/ as is
wraith.json the manifest
package.json
.wraith/types/ typings, made for you
dist/ the build, what Wraith loads

Wraith’s API is described in schemas, the same as the game’s types, and every language gets it from them. It’s all in the Wraith namespace, and it comes in areas, one for each thing Wraith does, like Wraith.Ui, Wraith.Input, Wraith.Hooks or Wraith.Tweaks. Each plugin gets its own of each, which stops working once the plugin’s unloaded.

The types an area uses are inside it, like Wraith.Window.Options for a window or Wraith.Tweaks.Definition for a tweak. The ones used everywhere, like Key, Chord, Vec3 and the errors, are right under Wraith. These docs name things the way the schemas do, like ui.addWindow or threads.toGame, and each tab shows how that’s written in its language.

Import what you use from the wraith module. It has Wraith, your plugin’s own namespace and the namespaces of plugins you depend on, like Freecam. None of them are globals, so a file that uses one imports it. Rename them as you like, like import { MyPlugin as game } from "wraith".

Areas are functions, like Wraith.ui(), and types and enums are under their names, like Wraith.Keybind.Phase.pressed. Each area also has instance(), so Wraith.Tweaks.instance() is the same as Wraith.tweaks(). Your editor’s autocomplete shows everything.

import { Wraith, MyPlugin, Freecam } from "wraith";
const ui = Wraith.ui();
const pressed = Wraith.Keybind.Phase.pressed;
const camera = Freecam.camera();

An imported namespace is a type too, so MyPlugin.Player works in a type annotation.

salamander is the other module, for native functions and snapshots, see below.

Workbench scripts and tweak sessions are the exception. They have every namespace and area as a global, with nothing to import.

This one adds a window, and a key that opens and closes it.

Your code in src/main.ts runs top to bottom when the plugin loads. There’s no start or main to export.

import { Wraith } from "wraith";
const window = Wraith.ui().addWindow({ title: "My Plugin", icon: "puzzle" }, () => {
Wraith.imgui().TextUnformatted("Hello!");
});
// Ctrl+F8
Wraith.input().bind({ id: "toggle", name: "Show the window", chord: { key: "f8", mods: 1 } }, (phase) => {
if (phase === Wraith.Keybind.Phase.pressed) window.setOpen(!window.isOpen());
});

Your code runs once as the plugin loads. If it fails, the plugin didn’t start, and the log says why.

Throwing while main.ts runs means the plugin didn’t start.

Plugins spend most of their time waiting, for a key, a file or the next frame. Wraith’s calls that take a while are async, and waiting on them never holds up the game. When one’s done you carry on on the thread you asked from, and hops like threads.toWorker move you.

They give promises, so await them, in an async function or at the top of main.ts.

import { Wraith } from "wraith";
await Wraith.threads().nextDraw();
// one frame later, on the render thread

Never block the render thread or the game thread, since the game waits for them. For slow work, hop to a worker and back. See Game and render threads.

You mostly don’t. When the plugin unloads, Wraith undoes everything it set up and puts the game’s code back.

There’s nothing to write. What your script made goes with it.

Settings players change, and files of your own, are on Settings and files.

Put schemas in schemas/ and they become types in your language, with the offsets baked in. Get the schema right in the Workbench first, then copy it in.

Your schemas are in your plugin’s own namespace, its id in PascalCase, so my_plugin gets MyPlugin and trails_horizon gets TrailsHorizon. Inside your .sal files you don’t write it, and Wraith’s types and those of plugins you depend on work by their short names too. See Namespaces for how names are found, and how to pick another namespace.

Code finds them through their types, like Player.heal for a method or addresses.player() for where something is, so there’s no name to type out as text.

They’re under your namespace from "wraith", fully typed. Every build regenerates the types, so they’re always in step with your .sal files.

import { MyPlugin } from "wraith";
const { Player } = MyPlugin;
const player = Player.at(address)!;
player.health = 100;
// A struct with [Instance] has the game's one
const main = Player.instance;
// Where things are, found the first time you ask
const healAt = Player.addresses.heal();
const playerAt = MyPlugin.addresses.player();

MyPlugin.addresses has your top-level statics and functions, and each struct’s addresses has its methods and statics, all but virtual ones.

Each field you read or set on player goes to the game’s memory right then, so it’s always current. That costs a little every time, which adds up in loops over lots of objects. For those, take a snapshot:

import { snapshot, save, edit } from "salamander";
const copy = snapshot(player); // one read of the whole struct
total += copy.health + copy.armor; // plain object reads, nothing touches memory
copy.health = 100;
save(copy); // writes only the fields you changed

A snapshot is a plain object. Structs and arrays inside it are plain too, and pointers stay live objects. It doesn’t follow memory, so take a new one when you want fresh values. save writes only what changed since the snapshot was taken, so fields the game changed meanwhile stay as they are, and it gives how many it wrote. Text fields aren’t written.

edit takes a snapshot, hands it to you, and saves it after, even if your function is async:

edit(player, (p) => {
p.health = 100;
p.pos.y += 2;
});

Plugins run in QuickJS, not Node or a browser. Modern JavaScript works, but APIs like fs and fetch aren’t there. npm packages that are plain JavaScript bundle in fine.

The only difference is the file. With --js you get src/main.js, and // @ts-check at the top still checks it against Wraith’s typings.

Made with ♥ by Asgard