mod

Your own mod: files, saved data, settings, exports.

mod is your mod's own table (every mod has a different one).

Fields

FieldTypeAccess
mod.idstringreadyour mod's id: its folder or zip name
mod.namestringreadthe name from mod.json
mod.versionstringreadthe version from mod.json
mod.exportstableread / writewhat other mods get with mods.get("yourid"): put functions and data here
mod.settingstablereadthe current values of your settings (see mod.setting)

Functions

# mod.setting(key, default, options?) → any

Declares a setting the player can change in the mod menu's Settings tab. Values are saved; read them from mod.settings (always current); the setting event tells you when one changed.

ParameterType
keystring
defaultboolean|number|stringits type sets the widget
options optionaltablelabel, help (tooltip), min + max (a slider), options (a list of strings: a drop-down)
Returns
anythe current value (saved from earlier, or the default)
mod.setting("count", 3, {label = "Raptors per wave", min = 1, max = 20})
mod.setting("hard", false, {label = "Hard mode"})
-- later:
for i = 1, mod.settings.count do world.spawnNear("velociraptor", 2500) end

# mod.save(value) → boolean|nil

Saves data for your mod (JSON in the game's save folder, moddata/.json). One value; use a table for many.

ParameterType
valueanytables, strings, numbers, booleans
Returns
boolean|niltrue, or nil and an error

# mod.load() → any

Loads what mod.save saved.

Returns
anywhat mod.save saved, nil when nothing
local data = mod.load() or {bestScore = 0}

# mod.read(path) → string|nil, string?

Reads a file of your mod (e.g. JSON data).

ParameterType
pathstringrelative to your mod's folder
Returns
string|nilthe contents
string?error
local waves = json.decode(mod.read("waves.json"))

# mod.exists(path) → boolean

Whether your mod has the file.

ParameterType
pathstring
Returns
boolean

# mod.files(folder?) → table

Lists files of your mod.

ParameterType
folder optionalstringdefault: the mod's folder
Returns
tablenames, sorted; folders end in /

# mod.writeData(name, text) → boolean|nil

Writes a file into your mod's data folder (moddata// in the save folder): logs, exported data, many save slots.

ParameterType
namestringa file name, may contain folders: "maps/arena1.json"
textstringthe contents (text or binary, up to 16 MB)
Returns
boolean|niltrue, or nil and an error

# mod.readData(name) → string|nil

Reads a file of your data folder.

ParameterType
namestring
Returns
string|nil

# mod.deleteData(name) → boolean

Deletes a file of your data folder.

ParameterType
namestring
Returns
boolean

# mod.dataFiles() → table

Lists the files of your data folder.

Returns
tablenames (with folders), sorted