Skip to content

Settings and files

Add your settings to Wraith’s own with settings.add(definition, changed). They’re listed under your plugin’s name in Wraith’s Settings, under Plugins and mods, where they’re searched, reset and saved for you. Your plugin gets a Settings button in the overlay’s Mods window, and under its button on the bar if it has menu items, which opens them there. Keys you bind are there too, and in All keys.

A Setting.Definition has an id, a name, and a kind: toggle, number, choice, text, colour or path. Numbers take min, max and step, choices take options, and paths take file, extension and placeholder. has_default and default_value give it a default, and scope set to game starts it just for this game. Players can switch any setting between every game and just this one in Settings.

import { Wraith } from "wraith";
const settings = Wraith.settings();
const exr = settings.add({ id: "exr", name: "Save an EXR too", kind: "toggle" }, null);
// A folder, picked with Browse or typed. Empty means the placeholder
const folder = settings.add({ id: "folder", name: "Folder", kind: "path", placeholder: "Pictures\\Wraith" }, null);
if (exr.get().toggle) { /* ... */ }

Each setting’s get and set take a Setting.Value, with a field for each kind: toggle, number, text for text, choices and paths, and colour. The second argument to add is called after it changes, on the thread that changed it, or leave it null.

For more than settings can show, the Settings button can open a window of yours instead, see The menu and the Settings button.

The files area reads and writes whole files. Each call runs on a worker and gives a promise, so it never stalls the game, and the game doesn’t see it. Wait on it from a task, not the render thread.

A path is relative to a root, or absolute:

  • data is your plugin’s folder for every game, like its saves.
  • game_data is one just for this game.
  • cache is for what’s safe to delete, like downloads.
  • plugin is your plugin’s files as installed, like its images. It’s read only, and a copy that goes once the plugin unloads.
  • game is the game’s folder, read only.

Your own folders are free to use. Anywhere else needs "files": true in "permissions" in your wraith.json, or the call fails with NotDeclared. A relative path can’t leave its root.

read(root, path, max_bytes) gives the bytes, up to 64 MiB when max_bytes is 0. write replaces a file whole, never leaving it half written, and makes its folders. There’s also append, list, stat, remove, move, makeDir, and resolve for the full path to show the player.

Images and other files you ship go in your plugin’s folder, and calls like images.load take paths relative to it.

files.pickFile and files.pickFolder show Windows’ own dialogs over the game, and give the full path, or null if the player cancels. What the player picks is yours to use until your plugin unloads, without "files": a file to open is yours to read, one to save to is yours to write, and you can use everything in a folder.

pickFile opens a file unless save is set. Every field of Files.FileDialog is optional: title, folder to start in, file_name to fill in, and kind and extension to narrow it to one type of file. Files.FolderDialog has a title and a folder.

Bytes are an array of numbers going in, and a Uint8Array coming out.

import { Wraith } from "wraith";
const files = Wraith.files();
const notes = [..."Hello!"].map((c) => c.charCodeAt(0));
await files.write("data", "notes.txt", notes);
const back = await files.read("data", "notes.txt", 0);
const path = await files.pickFile({ save: true, kind: "Text", extension: ".txt", file_name: "notes.txt" });
if (path !== null) await files.write("data", path, notes);

For the game’s own files, see The game’s files.

Made with ♥ by Asgard