What LuaAPI is
LuaAPI is a set of Lua scripts located in the Data\LuaAPI folder of GameServer. GameServer runs the file Data\LuaAPI\Main.lua at startup, then calls callback functions (onPlayerLogin, onTimerProcessSecond...) when events happen in the game. Scripts use functions provided by GameServer (the groups Player, Item, Map, DB, Message...) to read / write game data.
GameServer has 4 separate Lua systems, each independent and described in its own documentation section:
| System | Folder / file | Used for | Page |
|---|---|---|---|
| Lua API | Data\LuaAPI | Game events, chat commands, timers, modules | This section (Lua API) |
| Lua UI | Data\Lua_Ui | The server side of custom UI windows on the client | Lua UI |
| Lua Skill | Data\Scripts\Skills | Damage formulas, number of hits, hit rate of skills | Lua Skill |
| Item bags and character formulas | Data\Scripts\Item\ItemBagScript.lua and the server's data script files | Attach item bag files to each trigger (box drop, monster kill...) and calculate character stats | Item bags, Data Lua scripts |
The Lua API section also has these pages: the protection limits in the LuaAPI.ini file (Limits configuration), the new functions and events (Additional functions and events) and how to run scripts written for the Lua API of another MU server (Running scripts written for another Lua API).
debug table, os.execute, os.remove, os.rename, os.tmpname, os.exit, io.popen, package.loadlib; load / loadstring / loadfile / dofile accept only Lua source text. Each machine has time and memory limits. Details are on the page Error handling and safety notes.Conditions for LuaAPI to run
LuaAPI and UI Lua are two Plugin features (keys LuaApi and UiLua). According to the GameServer source code, a Plugin feature runs only when both of the following conditions are true:
- The feature is in the list that START reports to GameServer as owned by this server (the signature of this list is checked before use; if it is invalid, all Plugin features are turned off).
- In the file
Data\Plugin\Plugin.ini, section[Plugin], the corresponding key is not0. If the key is missing it is treated as1.
Key in [Plugin] | Value | Default | Meaning |
|---|---|---|---|
LuaApi | 0 / 1 | 1 | Load LuaAPI (Data\LuaAPI\Main.lua) |
UiLua | 0 / 1 | 1 | Load UI Lua (Data\Lua_Ui\Main.lua) |
[Plugin]
LuaApi = 1
UiLua = 1
Skill formulas and item bags do not depend on Plugin.ini: GameServer always loads them at startup and when the corresponding reload button is pressed.
When one of the two conditions is false, GameServer writes a line of the form LuaAPI disabled (Shop Plugin LuaApi not owned or switched off) (or Lua UI disabled ...) to the log and does not run the scripts.
Reloading scripts (no GameServer restart needed)
The GameServer control window has the following buttons:
| Button | What it reloads |
|---|---|
| Reload LuaAPI | Data\Commands\LuaCommands.xml and Data\LuaAPI\Main.lua with its modules. If LuaApi is switched off, this button shuts down the Lua machine and writes "disabled" to the log. |
| Reload Lua UI | Only the UI Lua machine (Data\Lua_Ui); LuaAPI is not touched. |
| Reload Plugin | Re-reads Plugin.ini: reloads both LuaAPI and UI Lua according to the new switches, together with the other Plugin parts. |
| Reload skills | Skill data and the Lua skill formulas (Data\Scripts\Skills). |
| Reload items / Lua | Item data and the item bag list (ItemBagScript.lua with the bag files). |
When the list of purchased features changes (START reports it again), GameServer also reloads LuaAPI and UI Lua automatically according to the new list.
What happens when LuaAPI is reloaded
- GameServer calls
onScriptUnloadof the old script, then destroys the whole old Lua machine. - All variables, tables and timers (
Timer) of the old script are lost. The temporary per-character data that LuaAPI keeps (actions blocked withPlayer.BlockAction, the table of extra stats, the last place of death, the last EXP received) is also cleared; the extra stats of characters currently playing are recalculated. - A new Lua machine is created and runs
Main.lua. IfMain.luahas a syntax error or an error while running, the Lua machine is closed and LuaAPI does not run until the next reload. - Loads
Data\Plugin\WebAPI\WebAPI.luaif this file exists (functions for the website to call). - Calls
onGameServerStart. - For each character currently playing, GameServer calls
onPlayerLoginagain (so modules know who is online).
onScriptUnload and create them again on onGameServerStart. See the page Error handling and safety notes.When GameServer shuts down
GameServer calls onScriptUnload (while the database connection is still open) and only then destroys the Lua machine. You can write data to the DB in this function.
Chat commands handled by Lua
Custom chat commands are declared in Data\Commands\LuaCommands.xml (group Type="16") and then handled in Lua. See the page Events, section onLuaCommand.
<CommandList>
<CommandSystem Type="16" Description="Custom Lua Comands">
<Command ID="2000" name="test" Description="" Authority="0"/>
</CommandSystem>
</CommandList>