What is interface Lua

Interface Lua is the server side of the custom interface windows on the client. A window is designed in EDITOR and exported as a file <Name>.ozg (together with the client-side file <Name>.lua) placed in the client's Data\Custom\Interface\GFx folder. When a player clicks a button in the window, the client sends a message (command + data) to the GameServer; the script in Data\Lua_Ui processes it and sends the result back.

Principle: the client only requests, the server decides. Never trust data sent by the client; always re-check it against server data.

Interface Lua has its own Lua engine, separate from LuaAPI: an error or reload on one side does not affect LuaAPI, and vice versa.

Conditions to run

According to the GameServer source code, interface Lua only runs when the Plugin feature UiLua is enabled: (1) START reports that this server owns the UiLua feature, and (2) in Data\Plugin\Plugin.ini, section [Plugin], the key UiLua is not 0 (a missing key is treated as 1). See Overview and enabling the feature.

[Plugin]
UiLua    = 1
The comments at the top of Data\Lua_Ui\Main.lua and the README.txt files in Lua_Ui\Includes still describe the old way of enabling it: Common.ini [Client] UiLua = 1. Those comments are outdated: the current GameServer code only checks the UiLua key in Plugin.ini as described above.

The Reload Lua UI button on the GameServer control window reloads this engine separately. If UiLua is off, the button only closes the Lua engine and writes the log Lua UI disabled.

Folder structure

Data\Lua_Ui\
  Main.lua                 root file: loads System, then the windows
  System\
    Constants.lua, Modules.lua, Callbacks.lua, CallbacksDB.lua, Timers.lua
    UI.lua                 window functions: UI.OnMessage, UI.Send, UI.Open...
  Includes\<WindowName>\     one folder per window (code, Config.lua, README.txt)

Loading works like LuaAPI: require("Folder\\File") looks in Data\Lua_Ui\Folder\File.lua and then Data\Lua_Ui\Includes\Folder\File.lua. Adding a new window = create a folder in Includes and add a line LoadModule("Name\\Name") at the end of Main.lua; a module that fails to load only breaks itself.

Sample window Status in Main.lua What it does
UiDemo On Example "Zu" window: the "info" button returns real character information; the "spin" button checks Zen and then returns a message.
demo1 On Place equipment from the inventory into the window's cells, press OK to exchange it for jewels (default Jewel of Bless x10 per item).
Exchangegems Off (commented out) A version that exchanges equipment for jewels, opened with the F10 key.

Function names with the Ui_ prefix

In interface Lua scripts, the functions / tables provided by the GameServer (the C++ part of LuaAPI) are used with the Ui_ prefix: Player.GetMoney becomes Ui_Player.GetMoney, Log.Add becomes Ui_Log.Add, DB.Query becomes Ui_DB.Query, LoadScript becomes Ui_LoadScript, Item.GetName becomes Ui_Item.GetName. Functions written in standard Lua (string, table, math...) keep their names. The UI table alone belongs to interface Lua (see Window functions UI.*).

Interface Lua does not have the Lua-written parts of LuaAPI. This engine only loads System\Constants.lua, Modules.lua, Callbacks.lua, CallbacksDB.lua, Timers.lua, UI.lua and the windows in Includes. Therefore there is no Library.lua (helper functions such as Helpers, Coin.GetByType / Check / Charge / Give, Scheduler, OptionBonus...) and none of the short names that LuaAPI defines in Lua (for example shortened names like GetObjectGuildNumber). If you need a function, write it yourself in the window's module.
  • Functions that take a file path (File.*, Config.*, LoadScript): relative paths are resolved from Data\Lua_Ui\ (whereas LuaAPI resolves from Data\LuaAPI\).
  • Ui_DB.Query returns its result to Ui_onDBQueryResult of the interface Lua engine (same rules as LuaAPI, retried up to 20 times, up to 5000 pending queries).

LuaAPI functions not available in interface Lua

Functions that only make sense for game events / automatic rules are removed from this engine:

  • The whole Event table (Event.Refresh)
  • Monster.WatchRespawn, Skill.Watch
  • Player.BlockAction, Player.IsActionBlocked
  • Player.GetOptionTable, Player.SetOptionTable

Interface Lua events

Interface Lua does not receive game events (monster kills, item pickups...) and does not receive chat commands; that is the job of LuaAPI. The functions the GameServer calls (defined in System\Callbacks.lua, CallbacksDB.lua, Timers.lua, UI.lua):

Function When Parameters / return
Ui_onGameServerStart() Loading finished (startup or Lua UI reload). Read-only: read the configuration.
Ui_onScriptUnload() Before a reload or GameServer shutdown. -
Ui_onUiMessage(aIndex, window, command, data) A client window sends a message (the player clicks a button). Already decoded by UI.lua and passed to UI.OnMessage. Game data writes allowed.
Ui_onNpcTalk(aIndex, npcIndex, npcClass) The player talks to an NPC (to open a window from an NPC). Called after LuaAPI's onNpcTalk. Return true (or a non-zero number) = "my window handled it", and the GameServer skips the original NPC dialog. Data writes allowed.
Ui_onItemPut(aIndex, window, cell, item) The player places an item from the inventory into one of the window's item cells. Return false = reject (the item stays in the inventory). No function = accept. Data writes allowed.
Ui_onDBQueryResult(label, param, result) Result of Ui_DB.Query. Return 1 = retry. If the query was created while handling a button, its result may also write game data.
Ui_onTimerProcessSecond/Minute/Hour/DayChange() Every second / minute / hour / day. Read-only + sending notifications.

Attach a module to an event with BridgeFunctionAttach(name, "FunctionName") (each function runs in its own pcall). The valid event names of this engine:

Attach name Corresponds to Receiving function
OnReadScript Ui_onGameServerStart ()
OnShutScript Ui_onScriptUnload ()
OnTimerThread every second ()
OnTimerMinute, OnTimerHour, OnTimerDayChange minute / hour / day ()
OnSQLAsyncResult Ui_onDBQueryResult (label, param, result); the first non-nil result is returned
OnNpcTalk Ui_onNpcTalk (aIndex, npcIndex, npcClass); return true or nil
OnItemPut Ui_onItemPut (aIndex, window, cell, item); if any function returns false, the item is rejected
OnUiMessage after UI.OnMessage (aIndex, window, command, data)

An event name not in this list is rejected and the log is not a Lua UI event is written. Unlike LuaAPI, here the attach name must exactly match the table.

When game data may be written

Game data (changing money, items, spawning monsters, teleporting...) may only be changed while handling a player's click action: in UI.OnMessage / Ui_onUiMessage, Ui_onNpcTalk, Ui_onItemPut, and in Ui_onDBQueryResult for queries created by them. Elsewhere (at load time, in timers) the script runs in restricted mode: it can only read data and send notifications.

Ui_Timer timers

The file System\Timers.lua provides the Ui_Timer table for timers in seconds (every second the GameServer calls Ui_onTimerProcessSecond to run due timers). The interval is rounded down to an integer, with a minimum of 1 second. Creating a timer with an existing name replaces the old timer. Timers may only read data and send notifications / UI.Send; they must not modify game data (that must be done while handling a button click). An error in one timer is logged and does not stop other timers.

Function Effect Returns
Ui_Timer.Create(seconds, name, function) Runs function once after the given number of seconds. true; false if function is not a function (logged).
Ui_Timer.CreateRepeating(seconds, name, function) Runs repeatedly every given number of seconds, without stopping until Remove. same as above
Ui_Timer.RepeatNTimes(seconds, name, count, function) Runs exactly count times and then deletes itself. function receives the parameter n = which run it is (1, 2, 3...). same as above
Ui_Timer.Remove(name) Deletes the timer. none
Ui_Timer.Exists(name) Whether the timer exists. true / false
Ui_Timer.GetRemaining(name) Seconds remaining until the next run. seconds; 0 if there is no timer
Ui_Timer.Stop(name) Pauses (keeps the remaining time). true; false if there is no timer
Ui_Timer.Start(name) Resumes a paused timer. true; false if there is no timer
Ui_Timer.IsActive(name) Whether the timer exists and is not paused. true / false
-- Count down 10 times, once per second
Ui_Timer.RepeatNTimes(1, "DemNguoc", 10, function(n)
	Ui_Log.AddC(1, "Lan thu " .. n)
end)

Item cell frames

A window can have item cells (created in EDITOR, grid or fixed-cell type, up to 16 columns x 16 rows). The player picks up an item in the inventory and clicks a cell to place it. That item leaves the inventory and is held in the GameServer's Chaos box until:

  • the script takes it with UI.BoxConsume (a successful exchange);
  • the player clicks the cell to take it back, closes the window, quits the game, or talks to an NPC: the item is returned to the inventory (if the inventory is full, it goes to the Gremory Case).

Item cells are not cleared when interface Lua is reloaded (they are the player's real items).

Limits

Limit Value
Maximum time for each GameServer call into a script 5 seconds (beyond that it is interrupted and logged)
Maximum memory of the interface Lua engine 128 MB
Maximum encoded data per message 900 bytes (UI.GetMaxData())
Maximum messages the client may send 10 per second per character (extra messages are dropped); the pending queue holds at most 20000
Window name and command name 1 - 31 characters made of letters, digits and _; system commands start with @ (used by the system)
Nested script calls up to 8 levels
DB query retries up to 20 times per query; up to 5000 pending queries

Script errors, timeouts and memory overruns are handled as in LuaAPI (see Error handling and safety notes).