Skip to content

Commit aecc7b4

Browse files
authored
Update Lua module docs for plugin exports (#73)
1 parent 2b194f1 commit aecc7b4

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

lua/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ As alluded to earlier, lua supports events and binds similar to the macro langua
165165

166166
In general, if you have some lua code from somewhere, you can simply `require('thescript')` and it will be usable in your code. The `fishb` example does this with an external behavior tree module. There is something special to consider about loading modules like this -- they can sometimes be quite large, and in order to prevent some errors the implementation of mq2lua turns off the normal frame yielding operation to load requires. What this means is that your client can hang when you require a file, especially if it is large. The good news is that lua caches requires, so it will only hang on the first load, and won't hang again until the plugin restarts.
167167

168+
MQ2Lua also supports modules provided by other plugins. Those modules are loaded the same way with `require('pluginname')`. See [Lua Modules from Plugins](../plugins/developing/lua-modules.md) for details.
169+
168170
There is another type of module that lua can include: compiled dlls. This is slightly more complex because you have to build it against the version of lua that we use in mq2lua. Luckily, this version of lua is in our vcpkg repo, so everything you need to link against is available in the mq git. LuaRocks is a great resource for these kinds of packages, and I have made detailed instructions for building these [here](./luarocks-modules.md).
169171

170172

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ nav:
7474
- plugins/developing/README.md
7575

7676
- Actors: plugins/developing/actors.md
77+
- Lua Modules: plugins/developing/lua-modules.md
7778

7879
- Core Plugins:
7980
- plugins/core-plugins/README.md

plugins/developing/lua-modules.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Lua Modules From Plugins
2+
3+
MQ2Lua can load Lua modules provided by other plugins via `require()`. This allows a plugin to expose native Lua tables, functions, and usertypes without using the TLO system.
4+
5+
## Lua Usage
6+
7+
```lua
8+
local myplugin = require("plugin.myplugin")
9+
10+
myplugin.hello()
11+
local value = myplugin.add(2, 3)
12+
```
13+
14+
## Plugin Authoring (C++)
15+
16+
### Build Setup
17+
18+
If your plugin uses the Lua interface, import the MQ2Lua props file:
19+
20+
```
21+
src/plugins/lua/LuaPlugin.props
22+
```
23+
24+
This adds the LuaJIT include path and links `lua51.lib`.
25+
26+
### Minimal Example
27+
28+
```cpp
29+
#include <mq/Plugin.h>
30+
#include <sol/sol.hpp>
31+
32+
PreSetup("MQ2MyPlugin");
33+
PLUGIN_VERSION(1.0);
34+
35+
extern "C" __declspec(dllexport) sol::object CreateLuaModule(sol::this_state s)
36+
{
37+
sol::state_view sv{ s };
38+
sol::table module = sv.create_table();
39+
40+
module.set_function("hello", []() {
41+
WriteChatf("Hello from MyPlugin!");
42+
});
43+
44+
module.set_function("add", [](int a, int b) { return a + b; });
45+
return sol::make_object(s, module);
46+
}
47+
```
48+
49+
### Usage Notes
50+
51+
- Lua requires the `plugin.` (or `plugin/`) prefix, e.g. `require("plugin.myplugin")`.
52+
- Plugin names are canonicalized (case-insensitive, `MQ`/`MQ2` prefix optional).
53+
- Module creation runs per Lua thread; do not share Lua tables across states.
54+
- If the owning plugin unloads, any Lua scripts that required its module will terminate.

0 commit comments

Comments
 (0)