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:

SystemFolder / fileUsed forPage
Lua APIData\LuaAPIGame events, chat commands, timers, modulesThis section (Lua API)
Lua UIData\Lua_UiThe server side of custom UI windows on the clientLua UI
Lua SkillData\Scripts\SkillsDamage formulas, number of hits, hit rate of skillsLua Skill
Item bags and character formulasData\Scripts\Item\ItemBagScript.lua and the server's data script filesAttach item bag files to each trigger (box drop, monster kill...) and calculate character statsItem 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).

These Lua machines do not share functions or variables. For example, a LuaAPI function cannot be called in a skill script, and vice versa.
Every Lua machine of GameServer (including the character stat scripts) runs in a restricted environment: there is no 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:

  1. 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).
  2. In the file Data\Plugin\Plugin.ini, section [Plugin], the corresponding key is not 0. If the key is missing it is treated as 1.
Key in [Plugin]ValueDefaultMeaning
LuaApi0 / 11Load LuaAPI (Data\LuaAPI\Main.lua)
UiLua0 / 11Load 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:

ButtonWhat it reloads
Reload LuaAPIData\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 UIOnly the UI Lua machine (Data\Lua_Ui); LuaAPI is not touched.
Reload PluginRe-reads Plugin.ini: reloads both LuaAPI and UI Lua according to the new switches, together with the other Plugin parts.
Reload skillsSkill data and the Lua skill formulas (Data\Scripts\Skills).
Reload items / LuaItem 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

  1. GameServer calls onScriptUnload of the old script, then destroys the whole old Lua machine.
  2. All variables, tables and timers (Timer) of the old script are lost. The temporary per-character data that LuaAPI keeps (actions blocked with Player.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.
  3. A new Lua machine is created and runs Main.lua. If Main.lua has a syntax error or an error while running, the Lua machine is closed and LuaAPI does not run until the next reload.
  4. Loads Data\Plugin\WebAPI\WebAPI.lua if this file exists (functions for the website to call).
  5. Calls onGameServerStart.
  6. For each character currently playing, GameServer calls onPlayerLogin again (so modules know who is online).
Monsters and NPCs created by modules are not remembered across a reload: the sample modules delete them when they receive 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>