Skip to content

Game and render threads

Two threads matter to a plugin. The render thread is where Wraith draws its overlay and your UI. The game thread is where the game’s own code runs. In most games they’re the same thread, but some engines, like Unity, run the game on a main thread and draw on another.

  • threads.onDraw runs once a frame on the render thread, inside an ImGui frame, so it can draw. Windows from ui.addWindow draw there too.
  • threads.onTick runs once a tick on the game thread. It can’t draw, but it can call into the game, like an engine’s functions that only work on its main thread.

Draws are for UI, ticks are for game logic. By default they’re the same thread. Each tick runs on the render thread just before its draw, so you get a tick then a draw, every frame, and there’s nothing to set up.

import { Wraith } from "wraith";
let health = 0;
Wraith.threads().onTick(() => {
// Read or change the game here
health = readHealth();
});
Wraith.ui().addWindow({ title: "Health", icon: "heart" }, () => {
Wraith.imgui().TextUnformatted(`${health}`);
});

Both give a subscription, and its cancel() stops it. Everything you subscribe goes when your plugin does. threads.onGameThread() says whether you’re on the game thread right now.

sub.cancel() stops one.

Code that waits can move between threads. The rule is the same in every language: when you wait on Wraith, you carry on on the thread that made the call. Only hops move you.

  • threads.toGame() carries on on the game thread at its next tick, or right away if you’re there already.
  • threads.toRender() carries on on the render thread, where drawing works.
  • threads.toWorker() carries on on a worker, for slow or blocking work.
  • threads.nextTick() carries on at the next tick, on the game thread.
  • threads.nextDraw() carries on at the start of the next frame, on the render thread.
  • threads.delay(ms) carries on after that many milliseconds, back on the thread that asked.

So after threads.nextDraw() you’re on the render thread, and after threads.toGame() everything you wait on carries on there till you hop again.

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

import { Wraith } from "wraith";
const threads = Wraith.threads();
let shown = 0;
async function watch() {
while (true) {
await threads.toGame();
const health = readHealth();
await threads.toRender();
shown = health;
await threads.nextDraw();
}
}
watch();

The game thread is only there while frames run, or while a plugin holds ticks. Otherwise hops to it fail with NoFrames.

A JavaScript or Lua plugin only runs one thing at a time, on one thread at a time, so a hop just changes where its next part runs. While a script waits, other scripts and the game carry on.

A plugin that knows the game’s own thread can take ticks over. It claims their lease with threads.claimTicks(), then calls threads.tick() from the game’s thread once a game frame, usually from a hook on the game’s update. The Unity plugin does this, so in Unity games onTick runs on Unity’s main thread. Other plugins’ onTick just runs there instead.

  • Claim as your plugin starts. Ticks then wait for your first tick(), so nothing runs on the wrong thread meanwhile.
  • tick() does nothing unless you hold the lease. With nobody holding it, ticks go back to the render thread.
  • If the holder goes 30 seconds without ticking, Wraith logs it.

Drive ticks from a compiled language. A script’s hook is skipped while the script’s busy, and its ticks would be too.

Scripts shouldn’t drive ticks, see above. Leave it to a compiled plugin for the game.

Both threads hold up the game while your code runs, so never block on them. Hop to a worker with threads.toWorker() for slow stuff.

Wraith never waits on a script from either thread, and await doesn’t block. If a tick comes while the script’s busy drawing on the render thread, it skips that tick rather than holding up the game, and a frame that finds it busy with a tick skips too. When ticks and frames share a thread they take turns, so it never happens. When a game drives ticks from its own thread, keep each tick’s work short and expect the odd one to be skipped.

Native plugins can run work in the background, named so the workbench’s Jobs panel lists it under your plugin. Name kinds of work, like index files, not each one.

  • threads.run(name, job) runs job once on a worker. It’s fine to block there, like on a download.
  • threads.startThread(name, main) gives a loop a thread of its own. main gets a Thread: return once thread.stopping() says so, and thread.sleep(ms) wakes early when you’re asked to stop. startThread gives a Running whose stop() asks and waits. Wraith also stops your threads as your plugin unloads, after onStopping, so you don’t have to.

Scripts run one thread at a time, so these fail with NotForScripts there. A script never needs to wait on a thread, since every slow call gives a promise: await it and Wraith carries on with other work meanwhile. For work that really needs a thread, use a native plugin.

Not for scripts. await slow calls instead, and hop with threads.toWorker() if you must.

Made with ♥ by Asgard