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.
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:
Global functions and variables work too. The main chunk runs once, when the module is loaded:
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:
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:
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 functionrequirecalls. When the main chunk raises an error,requireraises 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,SessionVarand so on) is available as usual.