Skip to content

Lua Modules

Any Lua file can become a module DLL. This is how you use autoplay2c with Setup Factory, but it works just as well for a library you share between AutoPlay projects.

a2c build tools.lua --target suf9-x86

This writes tools.dll next to tools.lua. A Lua file doesn't say which host it's for, so the target is required.

Writing a Module

A module is an ordinary Lua file. Whatever its main chunk returns is what require returns, just like a module in a .lua file:

local tools = {};

function tools.Greet(name)
    return "Hello " .. name .. "!";
end

return tools;

Global functions and variables work too. The main chunk runs once, when the module is loaded:

function Greet(name)
    return "Hello " .. name .. "!";
end

Loading a Module

The module name is what require loads, and by default it's the Lua file's name, so tools.lua gives tools. Put the DLL where the host's require looks for it, and load it:

local tools = require "tools";
Dialog.Message("Notice", tools.Greet("World"));

Add the DLL to your setup as a primer file or install it to a temporary folder, add that folder to package.cpath and require the module:

package.cpath = SessionVar.Expand("%TempLaunchFolder%") .. "\\?.dll;" .. package.cpath;
local tools = require "tools";

Put the DLL in CD_Root\AutoPlay\Scripts and require it from your global functions:

tools = require "tools";

Note

The module name must be a valid Lua name, which means letters, digits and _, not starting with a digit. If your file name isn't one, change the module name with --module (or Module name in the application). The DLL's file name must match the module name, because require finds the module by its file name.

Project Files

You can keep the settings of a module in a project file as well. In the application, choose the Lua file as the input, set the target and any other options, and save. A project file is also the only way to give a module your own version information.

What Isn't Available

A plain module doesn't have a project, so the project features don't apply. There's no rewritten project, no data folder replacement and no serialized objects.

Technical details
  • The DLL exports luaopen_<module>, the function require calls. When the main chunk raises an error, require raises it.
  • Run-time errors name the Lua file and line, as tools.lua:12:.
  • The module runs in the host's Lua state with the host's globals, so everything the host offers (Dialog, File, SessionVar and so on) is available as usual.