Skip to content
microlith57 edited this page Jun 30, 2023 · 7 revisions

Lönn supports writing custom libraries and putting them in the libraries folder. Libraries can help with reducing code duplication by putting shared code into a single place. Any plugin will be able to access and use the functions defined in those libraries, even plugins from other mods (any mod using such a library will have to add the mods containing the library as a dependency, of course). This also enables the release of pure "helper" plugins that assist with the making of other Lönn plugins.

The structure of a library is identical to the structure of any other Lua library:

local exampleLib = {}

if some_error then
    -- you can return nil to signal that some sort of problem has occurred
    return
end

exampleLib.some_key = 123

function exampleLib.some_func(a, b)
    return a + b
end

return exampleLib

You can access a library by using mods.requireFromPlugin():

local mods = require("mods")
local customStuff = mods.requireFromPlugin("libraries.custom_stuff")

To load a library from another mod, you need to specify the mod name:

local mods = require("mods")
local otherCustomStuff = mods.requireFromPlugin("libraries.other_custom_stuff", "OtherHelper")