Settings and files
Settings
Section titled “Settings”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 placeholderconst folder = settings.add({ id: "folder", name: "Folder", kind: "path", placeholder: "Pictures\\Wraith" }, null);if (exr.get().toggle) { /* ... */ }local Wraith = require("wraith").Wraith
local settings = Wraith.settings()
local exr = settings:add({ id = "exr", name = "Save an EXR too", kind = "toggle" }, nil)-- A folder, picked with Browse or typed. Empty means the placeholderlocal folder = settings:add({ id = "folder", name = "Folder", kind = "path", placeholder = "Pictures\\Wraith" }, nil)if exr:get().toggle then --[[ ... ]] endvar settings = Settings.Instance;var exr = settings.Add(new Setting.Definition { Id = "exr", Name = "Save an EXR too", Kind = Setting.Kind.Toggle }, null)!;// A folder, picked with Browse or typed. Empty means the placeholdervar folder = settings.Add(new Setting.Definition { Id = "folder", Name = "Folder", Kind = Setting.Kind.Path, Placeholder = "Pictures\\Wraith" }, null)!;if (exr.Get().Toggle) { /* ... */ }auto settings = wraith::api().settings();auto exr = settings.add({.id = sal::String::of("exr"), .name = sal::String::of("Save an EXR too"), .kind = Wraith::Setting::Kind::toggle}, {});// A folder, picked with Browse or typed. Empty means the placeholderauto folder = settings.add( {.id = sal::String::of("folder"), .name = sal::String::of("Folder"), .kind = Wraith::Setting::Kind::path, .placeholder = sal::String::of("Pictures\\Wraith")}, {});if (exr) { if (auto value = exr->get(); value && value->toggle) { /* ... */ }}The second argument is the Setting::Changed to call, or {} for none.
use salamander::Str;use wraith::{SettingChanged, SettingDefinition, SettingKind};
let settings = wraith::api().settings();let exr = settings .add(SettingDefinition { id: Str::new("exr"), name: Str::new("Save an EXR too"), kind: SettingKind::TOGGLE, ..Default::default() }, SettingChanged::default()) .ok()?;// A folder, picked with Browse or typed. Empty means the placeholderlet folder = settings .add( SettingDefinition { id: Str::new("folder"), name: Str::new("Folder"), kind: SettingKind::PATH, placeholder: Str::new("Pictures\\Wraith"), ..Default::default() }, SettingChanged::default(), ) .ok()?;if exr.get().is_ok_and(|v| v.toggle) { /* ... */ }The second argument is the SettingChanged to call, or SettingChanged::default() for none.
const settings = ctx.wraith.settings();const exr = try settings.add(.{ .id = "exr", .name = "Save an EXR too", .kind = .toggle }, null);// A folder, picked with Browse or typed. Empty means the placeholderconst folder = try settings.add(.{ .id = "folder", .name = "Folder", .kind = .path, .placeholder = "Pictures\\Wraith" }, null);if ((try exr.get()).toggle) { ... }Wraith_Settings settings = Wraith_Api_settings(w);Wraith_Setting_Definition exr_definition = { .id = SAL_STR("exr"), .name = SAL_STR("Save an EXR too"), .kind = Wraith_Setting_Kind_toggle };Wraith_Setting exr;Wraith_Settings_add(settings, exr_definition, (Wraith_Setting_Changed){ 0 }, &exr);/* A folder, picked with Browse or typed. Empty means the placeholder */Wraith_Setting_Definition folder_definition = { .id = SAL_STR("folder"), .name = SAL_STR("Folder"), .kind = Wraith_Setting_Kind_path, .placeholder = SAL_STR("Pictures\\Wraith") };Wraith_Setting folder;Wraith_Settings_add(settings, folder_definition, (Wraith_Setting_Changed){ 0 }, &folder);Wraith_Setting_Value value;if (Wraith_Setting_get(exr, &value) == SAL_OK && value.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.
Your own files
Section titled “Your own files”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:
datais your plugin’s folder for every game, like its saves.game_datais one just for this game.cacheis for what’s safe to delete, like downloads.pluginis your plugin’s files as installed, like its images. It’s read only, and a copy that goes once the plugin unloads.gameis 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.
Picking files and folders
Section titled “Picking files and folders”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);Bytes are a string.
local Wraith = require("wraith").Wraith
task.spawn(function() local files = Wraith.files() files:write("data", "notes.txt", "Hello!"):await() local back = files:read("data", "notes.txt", 0):await()
local path = files:pickFolder({ title = "Where screenshots go" }):await() if path then folder:set({ text = path }) endend)var files = Files.Instance;
await files.WriteAsync(Files.Root.Data, "notes.txt", "Hello!"u8.ToArray());using var back = await files.ReadAsync(Files.Root.Data, "notes.txt", 0);var bytes = back.Bytes();
var path = await files.PickFileAsync(new Files.FileDialog { Save = true, Kind = "Text", Extension = ".txt", FileName = "notes.txt" });const files = ctx.wraith.files();try sdk.tasks.wait(files.write(.data, "notes.txt", "Hello!"));const back = try sdk.tasks.wait(files.read(.data, "notes.txt", 0));defer back.release();// back.bytes()
if (try sdk.tasks.wait(files.pickFolder(.{ .title = .init("Where screenshots go") }))) |path| { // path is the full path}On top, ctx.config(T) loads a struct of your own from data, and ctx.gameConfig(T) from game_data, saving it as it changes. Every field in T needs a default. They’re Zig only, and aren’t listed in Wraith’s Settings. Other languages use settings.add for that.
For the game’s own files, see The game’s files.
wraith.io · Mods on the Hub · Get Wraith
Made with ♥ by Asgard
