Skip to content

Commit 610627a

Browse files
authored
Update lua plugin module export example to bool + out sol::object (#75)
* Update lua plugin module export example to bool + out sol::object * Refactor Lua module example for clarity and consistency * Apply suggestions from code review
1 parent 848969c commit 610627a

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

plugins/developing/lua-modules.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,43 @@ src/plugins/lua/LuaPlugin.props
2323

2424
This adds the LuaJIT include path and links `lua51.lib`.
2525

26-
### Minimal Example
26+
### Example
2727

28+
#### Plugin
2829
```cpp
2930
#include <mq/Plugin.h>
3031
#include <sol/sol.hpp>
3132

32-
PreSetup("MQ2MyPlugin");
33-
PLUGIN_VERSION(1.0);
33+
PreSetup("MQLuaModuleTest");
34+
PLUGIN_VERSION(0.1);
3435

35-
PLUGIN_API sol::object CreateLuaModule(sol::this_state s)
36+
void EchoPrimitive(const int value)
3637
{
37-
sol::state_view sv{ s };
38-
sol::table module = sv.create_table();
38+
WriteChatf("[LuaModuleTest] EchoPrimitive called with value=%d", value);
39+
}
40+
41+
PLUGIN_API bool CreateLuaModule(const sol::this_state luaState, sol::object& outModule)
42+
{
43+
sol::state_view lua(luaState);
44+
sol::table module = lua.create_table();
3945

40-
module.set_function("hello", []() {
41-
WriteChatf("Hello from MyPlugin!");
42-
});
46+
module["version"] = 1;
47+
module["EchoPrimitive"] = &EchoPrimitive;
4348

44-
module.set_function("add", [](int a, int b) { return a + b; });
45-
return sol::make_object(s, module);
49+
outModule = module;
50+
return true;
4651
}
4752
```
4853
54+
#### Script
55+
```lua
56+
local module = require("plugin.MQLuaModuleTest")
57+
printf("\ag[LuaModuleTest Script] Loaded Module version %d", module.version)
58+
print("\ay[LuaModuleTest Script] Calling EchoPrimitive(42)")
59+
module.EchoPrimitive(42)
60+
print("\ag[LuaModuleTest Script] Exercise complete")
61+
```
62+
4963
### Usage Notes
5064

5165
- Lua requires the `plugin.` (or `plugin/`) prefix, e.g. `require("plugin.myplugin")`.

0 commit comments

Comments
 (0)