Skip to content

Lua Environment

Noah edited this page Aug 24, 2023 · 2 revisions

This page describes available APIs and special methods you can implement in your init.lua.

Special Functions

The following functions can be implement in your init.lua and are called by the loader if they exist.

ready

This function is called after all game classes have loaded.

Example (init.lua)
function ready()
  -- code to run on ready
end

unload

This function is called when your mod is unloaded from the mod-manager.
You should clean up all changes your mod made to the game here.

Example (init.lua)
function unload()
  -- restore any changes made to the game here
end

It is recommended to make use of this function when implementing a mod that does not require a restart to be loaded/unloaded.

load

There is no special function that is called when the mod is loaded, instead all the code inside of the init.lua that is not inside a function will be called.

Example (init.lua)
function ready()
  -- not called on load
end

print("Im called on load!")

require

The require() call when used from a mod differs from lua's default implementation.

First off, all calls to require() are partially sandboxed by default, meaning you can only require files that are in the same directory of the directory your init.lua resides in.

Sub-Directories are also supported: <mod-folder>/a/b.lua would become require('a.b').

Game Files

In case you want to require files used by the game (i.e. classes.modules.interface.menuBuilder) you should prefix the module name with :.

Example
local menu_builder = require(":classes.modules.interface.menuBuilder")

Bypassing Hooks

In some cases other mods might intercept your call to require the game file and modify it in such a way that you can't properly work with it.
To circumvent this, prefix the require with !.

Example
local menu_builder = require(":!classes.modules.interface.menuBuilder")

Utility Mods

Some mods are "utility" mods, meaning they don't actively modify the game but are instead intended to be used by other mods.
In case you depend on such a mod, you can access it's files by writing your require as @<name>:<file>.

<Game-Dir>/mods/util_mod/util.lua

<Game-Dir>/mods/your_mod/init.lua

local T = {}

function T:some_util_func()
  -- ...
end

return T
local util = require("@util_mod:util")

util:some_util_func()
Clone this wiki locally