Module · Scripting core
mod
This mod: identity, lifecycle, exports for other mods, and the text and JSON files it ships with. Fields: mod.id, mod.name, mod.version, mod.side ("client" or "server"), mod.server, mod.client (booleans), mod.api.
mod.exists(path: string): boolean
True when the mod contains the file.
mod.export(exports: table)
Publishes a table other mods can read with mods.get once they list this mod as a dependency.
mod.export({ version = 2, spin = function(speed) return speed * 2 end })mod.has(name: string): boolean
True when a module or function exists in this script, e.g. "ui", "net.handle" or "building.onSelectionChanged". API tables are strict — reading a name that does not exist raises an error — so feature tests use this instead of `if ui.something then`. Unavailable modules (ui on a server, net outside a session) are nil globals, so `if ui then` keeps working too.
if mod.has("building.onSelectionChanged") then
building.onSelectionChanged(refresh)
else
timer.every(0.25, refresh)
endmod.hasPermission(permission: string): boolean
Whether this mod holds a permission, e.g. "ui.game" or "http:api.example.com".
mod.isClient(): boolean
True in client scripts. Same as the field mod.client.
mod.isServer(): boolean
True in server scripts (including the host's own server). Same as the field mod.server.
mod.list(pattern?: string): string[]
Lists the mod's files matching a pattern; * matches within a folder, ** across folders.
for _, file in ipairs(mod.list("maps/*.json")) do log.info(file) endmod.onUnload(handler: function)
Runs handler when the mod unloads — on reload, when disabled, or when leaving a server. Everything created through the API is removed automatically; use this for your own bookkeeping.
mod.onUnload(function() storage.set("lastRun", time.unix()) end)mod.readJson(path: string): any
Reads and parses a JSON file from the mod.
local waves = mod.readJson("data/waves.json")mod.readText(path: string): string
Reads a text file from the mod (.lua, .json, .txt, .md, .csv, .uxml, .uss).
path: Path inside the mod, e.g. "data/names.txt".
Returns The file's text.