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. Script source that JS or Lua plugins
require. - Services. Objects other plugins call, in any language.
Libraries
Section titled “Libraries”A Zig plugin can hand script plugins code to require, until it goes.
try sdk.wraith.root().?.libraries().add("freecam-math", "lua", @embedFile("math.lua"));Services
Section titled “Services”A service is a trait in your schemas marked [Service]. It says what the other plugins can call:
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 from your plugin struct. List a function in services for each one. Its return type says which trait it’s for:
const schema = @import("schema");const rt = sdk.salamander.rt;
const Camera = struct { speed: f32 = 1,
pub fn setSpeed(self: *Camera, speed: f32) void { self.speed = speed; } ...};
pub const Plugin = struct { camera: Camera = .{},
pub const services = .{freecam};
pub fn freecam(self: *Plugin) schema.Freecam { return .init(&self.camera, rt.implement(schema.Freecam, Camera)); } ...};Use one from a plugin you depend on:
if (sdk.salamander.service(schema.Freecam, "freecam")) |cam| cam.setSpeed(2);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
