Tweaks from plugins
A plugin can add tweaks to the overlay’s Tweaks window, the same list tweak files go in, with fields players can change. They’re listed under the plugin’s name.
For game values players should see and change, tagging a struct in your schemas [Tweak("..")] is often enough, with no code at all. See Values players can change.
Adding one
Section titled “Adding one”tweaks.add(definition, handler) adds one. tweaks.addIn(section, definition, handler) puts it under a heading of its own, like Combat.
A Tweaks.Definition has an id, a title, a default key like F8, a mode and its fields. Each Tweaks.Field has a name, a type like f32 or u32, a starting value, and limits with has_min, min, has_max and max.
The handler gets a Tweaks.Event. Its kind is on and off as it’s turned on and off, and changed for each field, with all of them coming just before on. With mode set to action it runs once each time instead, as on. Failing an on keeps it off, and the overlay shows why.
import { Wraith } from "wraith";
const { EventKind } = Wraith.Tweaks;
let x = 2;Wraith.tweaks().add( { id: "speed", title: "Speed", key: "F8", fields: [{ name: "x", type: "f32", value: x, has_min: true, min: 1, has_max: true, max: 5 }] }, (event) => { if (event.kind === EventKind.changed) x = event.value ?? x; else if (event.kind === EventKind.on) { /* turned on */ } else if (event.kind === EventKind.off) { /* turned off */ } },);Throwing from on keeps it off, and the overlay shows what was thrown.
local Wraith = require("wraith").Wraith
local EventKind = Wraith.Tweaks.EventKind
local x = 2Wraith.tweaks():add( { id = "speed", title = "Speed", key = "F8", fields = { { name = "x", type = "f32", value = x, has_min = true, min = 1, has_max = true, max = 5 } } }, function(event) if event.kind == EventKind.changed then x = event.value elseif event.kind == EventKind.on then --[[ turned on ]] elseif event.kind == EventKind.off then --[[ turned off ]] end end)Erroring from on keeps it off, and the overlay shows the error.
var tweaks = Tweaks.Instance;
double x = 2;tweaks.Add( new Tweaks.Definition { Id = "speed", Title = "Speed", Key = "F8", Fields = [new Tweaks.Field { Name = "x", Type = Tweaks.FieldType.F32, Value = x, HasMin = true, Min = 1, HasMax = true, Max = 5 }], }, e => { if (e.Kind == Tweaks.EventKind.Changed) x = e.Value; else if (e.Kind == Tweaks.EventKind.On) { /* turned on */ } else if (e.Kind == Tweaks.EventKind.Off) { /* turned off */ } });With Mode = Tweaks.Mode.Action it runs once each time. Throwing from On keeps it off, and the overlay shows what was thrown. A schema’s InstanceOrFail() throws when the game hasn’t made it yet, like before a save’s loaded.
static double x = 2;
static const Wraith::Tweaks::Field fields[] = { {.name = sal::String::of("x"), .type = Wraith::Tweaks::FieldType::f32, .value = 2, .has_min = true, .min = 1, .has_max = true, .max = 5},};
// in startWraith::Tweaks::Definition definition{ .id = sal::String::of("speed"), .title = sal::String::of("Speed"), .key = sal::String::of("F8"), .fields = sal::Slice<Wraith::Tweaks::Field>::of(fields),};auto added = wraith::api().tweaks().add(definition, sal::keep<Wraith::Tweaks::Handler>([](Wraith::Tweaks::Event event) -> sal::Result<void, Wraith::TweakError> { if (event.kind == Wraith::Tweaks::EventKind::changed) x = event.value; else if (event.kind == Wraith::Tweaks::EventKind::on) { /* turned on */ } else if (event.kind == Wraith::Tweaks::EventKind::off) { /* turned off */ } return {};}));With .mode = Wraith::Tweaks::Mode::action it runs once each time. Giving an error from on, like Wraith::TweakError::Failed, keeps it off, and the overlay shows why. A schema’s instance() is null when the game hasn’t made it yet, like before a save’s loaded.
use std::sync::Mutex;
use salamander::{Slice, Str};use wraith::{TweakError, TweaksDefinition, TweaksEvent, TweaksEventKind, TweaksField, TweaksFieldType, TweaksHandler};
static X: Mutex<f64> = Mutex::new(2.0);
static FIELDS: [TweaksField; 1] = [TweaksField { name: Str::new("x"), r#type: TweaksFieldType::F32, value: 2.0, has_min: true, min: 1.0, has_max: true, max: 5.0, ..unsafe { std::mem::zeroed() }}];
// in startlet definition = TweaksDefinition { id: Str::new("speed"), title: Str::new("Speed"), key: Str::new("F8"), fields: Slice::of(&FIELDS), ..Default::default()};wraith::api() .tweaks() .add(definition, TweaksHandler::keep(|event: TweaksEvent| -> Result<(), TweakError> { if event.kind == TweaksEventKind::CHANGED { *X.lock().unwrap() = event.value; } else if event.kind == TweaksEventKind::ON { // turned on } else if event.kind == TweaksEventKind::OFF { // turned off } Ok(()) })) .ok()?;With mode: TweaksMode::ACTION it runs once each time. Giving an error from ON, like TweakError::FAILED, keeps it off, and the overlay shows why. A schema’s instance() is null when the game hasn’t made it yet, like before a save’s loaded.
ctx.wraith.tweaks().add works as in other languages. sdk.tweaks also has helpers that take a struct for the fields, which are Zig only.
// Runs once each time, with its fields as a structconst GiveMira = struct { amount: u32 = 10000, pub const fields = .{ .amount = .{ .min = 1 } };};
fn giveMira(self: *Plugin, args: GiveMira) !void { _ = self; _ = (try schema.GameStateManager.instanceOrFail()).giveMira(args.amount);}
// in starttry sdk.tweaks.action(ctx, .{ .id = "give_mira", .title = "Give Mira", .section = "Money" }, GiveMira, self, giveMira);Each field in the struct is one in the overlay, starting at its default. Numbers are boxes, bools checkboxes and enums dropdowns. fields can say more, like .min, .max, .label, .hex or .key.
sdk.tweaks.toggle is on and off with fields, taking .{ .on = on, .off = off }. on(self, args) runs as it’s turned on, off(self) as it’s turned off, and an optional changed(self, args) runs when a field changes while it’s on.
If a callback fails, the tweak stays off and the overlay shows why. A schema’s instanceOrFail() fails with “not there yet” when the game hasn’t made it, like before a save’s loaded.
static double x = 2;
static uint32_t speed(uintptr_t self, Wraith_Tweaks_Event event) { (void)self; if (event.kind == Wraith_Tweaks_EventKind_changed) x = event.value; else if (event.kind == Wraith_Tweaks_EventKind_on) { /* turned on */ } else if (event.kind == Wraith_Tweaks_EventKind_off) { /* turned off */ } return SAL_OK;}
static const Wraith_Tweaks_Handler_table speed_table = { SAL_TABLE(Wraith_Tweaks_Handler), .call = speed };
static const Wraith_Tweaks_Field fields[] = { { .name = SAL_STR_INIT("x"), .type = Wraith_Tweaks_FieldType_f32, .value = 2, .has_min = true, .min = 1, .has_max = true, .max = 5 },};
/* in start */Wraith_Tweaks_Definition definition = { .id = SAL_STR("speed"), .title = SAL_STR("Speed"), .key = SAL_STR("F8"), .fields = { fields, 1 },};Wraith_Tweaks_add(Wraith_Api_tweaks(w), definition, (Wraith_Tweaks_Handler){ 0, &speed_table });With .mode = Wraith_Tweaks_Mode_action it runs once each time. Returning an error code from on, like Wraith_TweakError_Failed, keeps it off, and the overlay shows why.
Hooks and windows as tweaks
Section titled “Hooks and windows as tweaks”Two kinds need no handler. tweaks.addHook(section, definition, hook) makes a toggle that enables a hook while it’s on. tweaks.addOpener(section, definition, window) makes an open button that opens and focuses one of your windows, so it can be opened from the Tweaks window too. The heading comes first, and "" is none. The definition’s mode and fields are ignored.
import { Wraith } from "wraith";
const tweaks = Wraith.tweaks();
tweaks.addHook("Money", { id: "free_shopping", title: "Free shopping" }, freeShopping);tweaks.addOpener("Party", { id: "party", title: "Party members" }, partyWindow);local Wraith = require("wraith").Wraith
local tweaks = Wraith.tweaks()
tweaks:addHook("Money", { id = "free_shopping", title = "Free shopping" }, free_shopping)tweaks:addOpener("Party", { id = "party", title = "Party members" }, party_window)var tweaks = Tweaks.Instance;
tweaks.AddHook("Money", new Tweaks.Definition { Id = "free_shopping", Title = "Free shopping" }, freeShopping);tweaks.AddOpener("Party", new Tweaks.Definition { Id = "party", Title = "Party members" }, partyWindow);Wraith::Hook free_shopping; // from hooks().detourNativeWraith::Window party_window; // from ui().addWindow
// in startauto tweaks = wraith::api().tweaks();tweaks.addHook("Money", {.id = sal::String::of("free_shopping"), .title = sal::String::of("Free shopping")}, free_shopping);tweaks.addOpener("Party", {.id = sal::String::of("party"), .title = sal::String::of("Party members")}, party_window);use salamander::Str;use wraith::{Hook, TweaksDefinition, Window};
fn add(free_shopping: Hook, party_window: Window) -> Option<()> { let tweaks = wraith::api().tweaks(); let shopping = TweaksDefinition { id: Str::new("free_shopping"), title: Str::new("Free shopping"), ..Default::default() }; tweaks.add_hook("Money", shopping, free_shopping).ok()?; let party = TweaksDefinition { id: Str::new("party"), title: Str::new("Party members"), ..Default::default() }; tweaks.add_opener("Party", party, party_window).ok()}ctx.wraith.tweaks().addHook and addOpener take Wraith’s own hooks and windows. For the SDK’s, sdk.tweaks.hook takes any hook from sdk.hooks, or anything with enable and disable, and sdk.tweaks.opens takes an sdk.ui.Window.
try ctx.wraith.tweaks().addOpener("Party", .{ .id = "party", .title = "Party members" }, self.party_window);
try sdk.tweaks.hook(ctx, .{ .id = "free_shopping", .title = "Free shopping", .section = "Money" }, &self.spend_mira);try sdk.tweaks.opens(ctx, .{ .id = "stats", .title = "Stats", .section = "Party" }, &self.stats.window);static Wraith_Hook free_shopping; /* from Wraith_Hooks_detourNative */static Wraith_Window party_window; /* from Wraith_Ui_addWindow */
/* in start */Wraith_Tweaks tweaks = Wraith_Api_tweaks(w);Wraith_Tweaks_Definition shopping = { .id = SAL_STR("free_shopping"), .title = SAL_STR("Free shopping") };Wraith_Tweaks_addHook(tweaks, SAL_STR("Money"), shopping, free_shopping);Wraith_Tweaks_Definition party = { .id = SAL_STR("party"), .title = SAL_STR("Party members") };Wraith_Tweaks_addOpener(tweaks, SAL_STR("Party"), party, party_window);For anything else that opens, a definition with mode set to open shows an open button too, and calls the handler’s on when it’s clicked.
Other tweaks
Section titled “Other tweaks”tweaks.isOn(name) says whether any tweak’s box is on, by its saved name like kai/Combat/Max bravery, and tweaks.setOn(name, on) turns it on or off next frame, the same as clicking it. tweaks.onSwitched(handler) hears every tweak going on or off. tweaks.remove(id) takes one of yours away.
import { Wraith } from "wraith";
Wraith.tweaks().onSwitched((name, on) => Wraith.log().info(`${name} is ${on ? "on" : "off"}`));local Wraith = require("wraith").Wraith
Wraith.tweaks():onSwitched(function(name, on) Wraith.log():info(name .. (on and " is on" or " is off")) end)var log = Log.Instance;Tweaks.Instance.OnSwitched((name, on) => log.Info($"{name} is {(on ? "on" : "off")}"));#include <string>
// in startwraith::api().tweaks().onSwitched(sal::keep<Wraith::Tweaks::Switched>([](std::string_view name, bool on) { wraith::api().log().info(std::string(name) + (on ? " is on" : " is off"));}));use wraith::TweaksSwitched;
wraith::api() .tweaks() .on_switched(TweaksSwitched::keep(|name: &str, on: bool| { wraith::api().log().info(&format!("{name} is {}", if on { "on" } else { "off" })); })) .ok()?;fn switched(self: *Plugin, name: []const u8, on: bool) void { _ = self; log.info("{s} is {s}", .{ name, if (on) "on" else "off" });}
// in start_ = try ctx.wraith.tweaks().onSwitched(.init(self, switched));static void switched(uintptr_t self, sal_string name, bool on) { (void)self; char text[256]; int n = snprintf(text, sizeof text, "%.*s is %s", (int)name.len, name.ptr, on ? "on" : "off"); Wraith_Log_info(Wraith_Api_log(w), (sal_string){ text, n > 0 ? (uintptr_t)n : 0 });}
static const Wraith_Tweaks_Switched_table switched_table = { SAL_TABLE(Wraith_Tweaks_Switched), .call = switched };
/* in start */Wraith_Tweaks_SwitchSubscription subscription;Wraith_Tweaks_onSwitched(Wraith_Api_tweaks(w), (Wraith_Tweaks_Switched){ 0, &switched_table }, &subscription);wraith.io · Mods on the Hub · Get Wraith
Made with ♥ by Asgard
