Skip to content

Sharing between plugins

Plugins can lean on each other, like one that knows an engine and others that use it. List the ones you need in depends in your manifest so they load first.

There are three ways to share, from simplest:

  • Schemas. A plugin that depends on another gets its schemas too, so it can use the same game types without copying them.
  • Libraries. Source code other scripts require, in the same language.
  • Services. Objects other plugins call, in any language.

A library is code other scripts load with require(name), and tweaks with @use name. It’s there until your plugin goes.

local wraith = require("wraith")
wraith.libraries:add("hello", "lua", "return function(name) return 'hi ' .. name end")

Libraries only work in their own language. Use a service to reach every language.

A service is a trait in your schemas marked [Service]. It says what the other plugins can call:

schemas/freecam.sal
struct Vec3 { f32 x; f32 y; f32 z; }
[Service] trait Freecam {
void setSpeed(f32 speed);
async Vec3 position();
void onMove(keep Mover mover);
}
trait Mover { void moved(Vec3 pos); }

Plugins that depend on yours get these types too, so both sides agree on them. Args and results are what the trait says, like numbers, strings, structs and other traits. A method marked async gives a promise.

Offer it by returning a table with the trait’s name from main.lua. Each method gets the object as self, then the args:

local speed = 1
local movers = {}
local Freecam = {}
function Freecam:setSpeed(s)
speed = s
end
function Freecam:position()
return { x = 1, y = 2, z = 3 }
end
function Freecam:onMove(mover)
movers[#movers + 1] = mover
end
return { Freecam = Freecam }

Use it by requiring the plugin by name:

local Freecam = require("freecam").Freecam
Freecam:setSpeed(2)
local pos = Freecam:position():await()
Freecam:onMove({ moved = function(self, pos) print(pos.x) end })

wraith sdk lua types types it for you, from the schemas of the plugins you depend on. It finds them next to your plugin’s folder, or installed.

Your plugin fails to load if it marks a trait [Service] and doesn’t give one, so a typo shows up straight away.

A plugin that depends on yours unloads before yours does, so its services never go away under it. Methods run on whatever thread called, so keep them quick.

Made with ♥ by Asgard