Globals and the sandbox

print, require, on, inspect, class and the standard library a mod gets.

Every script mod runs in its own sandbox: a private global table with the safe parts of Lua's standard library and the game API. Globals you create are yours alone; another mod cannot see or break them (use mod.exports and mods.get to share). The API tables (world, ui, ...) are copies, so replacing world.spawn in your mod changes only your own copy.

Available: assert error ipairs next pairs pcall xpcall select tonumber tostring type rawequal rawget rawset rawlen setmetatable getmetatable unpack _VERSION, the libraries string table math utf8 coroutine, os.clock os.time os.date os.difftime and debug.traceback.

Not available: io, the rest of os (no execute, remove, exit, getenv), package, dofile, loadfile, the rest of debug, binary chunks. A mod reads files from its own folder with mod.read, and saves data with mod.save / mod.load.

Limits: a single call into a mod (an event handler, a timer, the main chunk) may run for 3 seconds (10 when the mod loads); after that it is stopped with the error "the script ran for too long (an endless loop?)". All mods share 1 GB of Lua memory. An event handler that fails 20 times in a row is removed (the console says so).

Functions

# print(...)

Writes a line to the console (`) and the game's log, prefixed with your mod's id: [mymod] hello.

ParameterType
...anyvalues, converted with tostring and separated by spaces
print("spawned", c, "at", c.x, c.z)

# require(name) → any

Loads a Lua file of your mod once and returns its result; later calls return the same value. The module runs in your mod's sandbox (its globals are your globals).

ParameterType
namestringa module of your mod: "util" loads util.lua, "ai.raptor" loads ai/raptor.lua (or ai/raptor/init.lua); "@othermod/name" loads a module from another enabled mod's folder
Returns
anywhat the module returned (true when it returned nothing)
-- util.lua:  local M = {} function M.double(x) return x * 2 end return M
local util = require("util")
print(util.double(21))  -- 42

# load(text, chunkname?, mode?, env?) → function|nil, string?

Compiles Lua code from a string (text only, never binary chunks).

ParameterType
textstringLua source code
chunkname optionalstringname shown in error messages
mode optionalstringignored: only text is accepted
env optionaltablethe chunk's global table (default: your sandbox)
Returns
function|nilthe compiled chunk
string?the error when it does not compile
local f = assert(load("return 1 + 2"))
print(f())  -- 3

# on(event, handler, priority?) → integer

Short for events.on: listen for a game event or a custom one.

ParameterType
eventstringan event name (see Events)
handlerfunctioncalled with the event table
priority optionalintegerhigher runs earlier (default 0)
Returns
integerthe listener id (for events.off)
on("kill", function(e)
  if e.byHunter then game.message("Got one: " .. e.creature.name) end
end)

# inspect(value, depth?) → string

Readable text of any value, with tables expanded: handy with print and in the console.

ParameterType
valueanyanything
depth optionalintegerhow deep tables are expanded (default 3)
Returns
stringreadable text
print(inspect({a = 1, list = {1, 2, 3}}))  -- { a = 1, list = { 1, 2, 3 } }

# class(parent?) → table

A minimal class helper. Call the class to make an instance; init is its constructor; Class.super is the parent.

ParameterType
parent optionaltablea class to inherit from
Returns
tablethe new class
local Boss = class()
function Boss:init(creature) self.c = creature; self.phase = 1 end
function Boss:enrage() self.c.scale = self.c.scale * 1.2 end
local b = Boss(world.nearest("trex"))

# collectgarbage(option?) → number

The safe subset of Lua's collectgarbage.

ParameterType
option optionalstring"count" (default), "step" or "collect"
Returns
numberLua memory in KB for "count"