Skip to content

Workbench panels

A plugin can add tabs to the Workbench, for tools that belong next to Wraith’s own, like the Unity plugin’s Unity tab with its Scene.

They’re advanced because of where they run. The Workbench is a window of its own, with a thread of its own, so a panel draws on a third thread: not the render thread or the game thread. Read that page first.

ui.addWorkbenchPanel(options, draw) takes WorkbenchPanel.Options and a draw function, and gives back the panel. Each panel is a tab of its own, a docked window like Wraith’s. Draw with the same ImGui calls as overlay windows. Don’t start a window of your own, since the panel’s already one.

import { Wraith } from "wraith";
const imgui = Wraith.imgui();
let party: string[] = [];
Wraith.threads().onTick(() => {
party = readParty();
});
const panel = Wraith.ui().addWorkbenchPanel({ title: "Party" }, () => {
for (const name of party) imgui.TextUnformatted(name);
});

Options says where it goes and what it is:

  • title: its tab’s label.
  • kind: what it is, like sheets, and id: which one, for several of a kind at once, like an address. Together they’re how Wraith remembers where it was moved, so keep them the same each time. An empty kind uses the title.
  • place: top, with the documents, bottom, with Addresses, or left, with Search. Only the first time it shows; after that it’s wherever it was left. It can be closed, and comes back from View, or when you focus() it.
  • order: lower comes first among tabs in the same place. Wraith’s own are 100 apart from 100, and 0 is after them.
  • parent: inside another tab instead, as one of its tabs, below.

It opens itself behind what you’re looking at.

A tab with parent goes inside the tab it names, as one of its tabs, like Scene and Classes in the Unreal tab. Name one of your own by its kind, like unreal, another plugin’s as plugin/kind, or Wraith’s Debug and Diagnostics as wraith/debug and wraith/diagnostics. It’s still a panel of its own: isOpen() says whether it’s the one in front, and focus() brings the tab it’s in to the front, on it.

The tab holding them is a WorkbenchTab, below, whose describe says parts. It draws nothing of its own, only the tabs that name it, in their order. In Zig, sdk.workbench.Holder is one:

holder: sdk.workbench.Holder = .{ .options = .{ .title = "Unreal", .kind = "unreal" } },
_ = try sdk.workbench.addHolder(&self.holder);
self.scene = try sdk.workbench.addPanel(self, drawScene, .{ .title = "Scene", .kind = "scene", .parent = "unreal" });

isOpen() says whether its tab’s showing, so you can skip work for a panel nobody’s looking at. focus() brings it to the front. remove() takes it out, and it goes anyway when your plugin does.

ui.showClass("Game.Player") opens a C# class in Classes, from anywhere.

ui.addWorkbenchTab(tab) takes an object implementing WorkbenchTab instead of a draw function, for a tab that says more about itself, or a kind of tab with several open at once, like Wraith’s memory views. Its methods run on the Workbench’s thread:

  • describe() is asked once: its title, kind, place, order and parent as above, multi for many at once, and parts to hold the tabs that name it as their parent.
  • draw(id) draws one. id is empty unless it’s multi.
  • status(id) gives its label and a dot for something not saved, each frame it shows.
  • create() is View → New’s, for multi: a new one’s id, or empty for none.
  • keepOpen(id) is its close button’s: true keeps it open, like to ask about saving first.

Only describe and draw are needed. Without the rest it keeps its label, has no New, and closes when asked. It gives back a Contribution, whose open(id) opens one of a multi tab, or brings it to the front, and close(id) closes one without asking. A multi tab’s ids are yours, like a file’s path or an address, and each one’s place is remembered by its kind and id.

import { Wraith } from "wraith";
let made = 0;
const notes = Wraith.ui().addWorkbenchTab({
describe() { return { title: "Notes", kind: "notes", place: "bottom", order: 0, multi: true }; },
draw(id) { /* ImGui calls */ },
status(id) { return { title: `Note ${id}`, unsaved: false }; },
create() { return String(++made); },
keepOpen(id) { return false; },
});
notes.open("1");

The game doesn’t know about the Workbench’s thread. Calling into it from there, like an engine’s functions or a C# method, can crash it, and reading something the game’s changing can give you half of one value and half of another.

So split it in two:

  1. Your game-thread code, like an onTick, reads what the panel shows and keeps it.
  2. The panel draws what was kept, and queues anything it wants changed.
  3. The game-thread code makes the changes on its next tick.

A JavaScript or Lua plugin only runs one thing at a time, so its panel and its onTick never run together. Keep what the panel shows in plain variables, like party above, and let the panel change variables your onTick acts on.

The Workbench waits its turn for your script, so a long onTick makes the panel slow, never the game.

Your plugin’s code really does run on several threads at once, so sdk.workbench has two helpers that make it safe without ever holding up the game.

Latest(T) hands the newest value from one thread to another. The game-thread side builds each value in next()’s allocator, which can hold slices, then publishes it. Publishing never waits: if the panel’s reading the last one right then, it gives false, and you try again next tick. The panel reads the newest and ends when it’s done.

Queue(A) goes the other way. The panel pushes, and the game-thread side drains, which never waits either.

party: sdk.workbench.Latest(Party) = .{},
edits: sdk.workbench.Queue(Edit) = .{},
pending: ?Party = null,
fn tick(self: *Plugin) void {
for (self.edits.drain()) |edit| apply(edit);
if (!self.panel.isOpen()) return;
if (self.pending) |p| {
if (self.party.publish(p)) self.pending = null;
return;
}
const p = readParty(self.party.next()) catch return;
if (!self.party.publish(p)) self.pending = p;
}
fn drawPanel(self: *Plugin) void {
const party = self.party.read() orelse return;
defer party.end();
for (party.value.members) |m| if (sdk.ui.button(m.name)) self.edits.push(.{ .heal = m.id });
}

The Unity plugin’s Scene panel works exactly like this, if you want a bigger example.

C# runs on several threads at once too, but there’s no Latest or Queue. Plain .NET does the same job. Build a new value on the game thread and swap it into a volatile field, and never change one after the panel could see it. Edits go the other way through a ConcurrentQueue, which never waits either.

volatile Party? party;
readonly ConcurrentQueue<Edit> edits = new();
void Tick()
{
while (edits.TryDequeue(out var edit)) Apply(edit);
if (panel.IsOpen()) party = ReadParty();
}
void DrawPanel()
{
if (party is not { } p) return;
foreach (var m in p.Members) if (imgui.Button(m.Name)) edits.Enqueue(new Edit(Heal: m.Id));
}

Swap in a new value whole, under a lock that’s only held for the swap. The game thread builds the next one before taking the lock, and the panel takes its copy and lets go before drawing, so neither waits on the other for more than a moment. Never change a value after the panel could see it. Edits go the other way through a list of your own, under the same lock, which the game thread takes and empties each tick.

A std::shared_ptr to a const value is the swap. The panel keeps the one it took alive while it draws, even if a newer one comes in.

#include <memory>
#include <mutex>
#include <string>
#include <vector>
struct Member {
uint32_t id;
std::string name;
};
using Party = std::vector<Member>;
struct Edit {
uint32_t heal;
};
static std::mutex lock;
static std::shared_ptr<const Party> party;
static std::vector<Edit> edits;
static Wraith::WorkbenchPanel panel;
Party read_party();
void apply(Edit edit);
// On the game thread, each tick
static void tick() {
std::vector<Edit> todo;
{
std::lock_guard held(lock);
todo.swap(edits);
}
for (auto edit : todo) apply(edit);
if (!panel.isOpen()) return;
auto next = std::make_shared<const Party>(read_party());
std::lock_guard held(lock);
party = next;
}
// On the Workbench's thread
static void draw_panel() {
std::shared_ptr<const Party> shown;
{
std::lock_guard held(lock);
shown = party;
}
if (!shown) return;
for (const auto& m : *shown) {
if (!wraith::api().imgui().Button(m.name.c_str())) continue;
std::lock_guard held(lock);
edits.push_back({.heal = m.id});
}
}

Whatever the language:

  • Copy what you need into the value the panel gets. A pointer into the game can be stale by the time the panel draws.
  • Check anything in an edit again before you use it, since the object it’s about may be gone by then.
  • A panel’s remove() waits for a draw under way, so it’s safe to free what the panel uses after it. Call it before tearing things down in stop. Don’t hold a lock your draw needs while you call it, or the two will wait on each other.

Made with ♥ by Asgard