Folder tree
Data\LuaAPI\
Main.lua root file, GameServer runs this file
System\ system part (do not edit, except Callbacks.lua to add your own code)
Constants.lua constants (MAP_*, CLASS_*, STAT_*, ACTION_*...)
FileReader.lua reads text files with columns (Reader:Load)
Modules.lua module system (BridgeFunctionAttach) and short function names
Callbacks.lua game event functions: onPlayerLogin, onPlayerLogout...
CallbacksDB.lua DB query results: onDBQueryResult
Timers.lua timers: Timer.*, onTimerProcessSecond...
Library.lua ready-made library: Helpers, Coin.*ByType, ActionTick, Scheduler...
Includes\ one folder per feature
<ModuleName>\
Config.lua configuration (edit this file)
<ModuleName>.lua running code (no need to edit)
README.txt module guide
<ModuleName>.sql (only some modules) creates tables in the game DB
Load order
Main.lua loads in a fixed order. Do not change the order of the System lines:
System\ConstantsSystem\FileReaderSystem\ModulesSystem\CallbacksSystem\CallbacksDBSystem\TimersSystem\Library- The modules via the
LoadModulefunction (in the order of the lines inMain.lua)
After Main.lua finishes running, GameServer calls onGameServerStart, then calls onPlayerLogin for the characters currently playing (see the page Overview).
Where require looks for files
Use require("Folder\\File") (two \\ characters in the Lua string). GameServer sets the search path to:
Data\LuaAPI\Folder\File.luaData\LuaAPI\Includes\Folder\File.lua
The Data folder is taken from the GameServer DataPath key if present. DLL files cannot be loaded (the cpath path is left empty).
require("System\\Constants") -- Data\LuaAPI\System\Constants.lua
require("Warehouse\\Config") -- Data\LuaAPI\Includes\Warehouse\Config.lua
Enabling / disabling a module
At the end of Main.lua there is a function LoadModule(name) that wraps require in pcall: a module that fails to load writes the log [Main.lua] Failed to load module ... and the other modules still load normally.
LoadModule("WelcomeMessage\\WelcomeMessage")
LoadModule("CmdCommands\\Commands")
LoadModule("NpcMessage\\NpcMessage")
LoadModule("CustomWindowName\\CustomWindowName")
-- LoadModule("CustomItemExchange\\CustomItemExchange")
-- LoadModule("DailyReward\\DailyReward")
How to do it:
- Enable: remove the
--at the start of the module'sLoadModuleline. - Disable: add
--at the start of the line, or setEnable = falsein the module'sConfig.lua(most modules have this key). - Press Reload LuaAPI in the GameServer control window.
Default state of the current installation: WelcomeMessage, CmdCommands, NpcMessage, CustomWindowName are enabled; the remaining modules are left commented out (disabled). The full list is on the page Sample modules.
Adding your own module
- Create the folder
Data\LuaAPI\Includes\NewName\with the fileNewName.lua(andConfig.luaif needed). - In that file, write the handler functions and attach them to events with
BridgeFunctionAttach(see Attaching a module to events). - Add the line
LoadModule("NewName\\NewName")toMain.lua. - Press Reload LuaAPI.
FileReader.lua
Reader:Load(path) opens a text file and reads it line by line into columns. Used for self-formatted data files:
- Columns are separated by spaces or Tabs; strings containing spaces are placed in
"quotes. - Only
//(two consecutive slashes) located outside the"quotes starts a comment: from there to the end of the line is ignored. A single/is still a normal character (for example the patha/b). Empty lines or lines with only a comment are skipped. GetLine(end_text)returnsfalseat the end of the file or when it meets a line starting withend_text.GetAsString()returns the next column (an empty string if there are no more columns);GetAsNumber()returns a number (a*column =-1, no more columns =0, a non-numeric column =nil, so check fornilbefore calculating).- Returns
nilif the file cannot be opened. CallClose()when done reading.
local r = Reader:Load(duong_dan_file)
if r then
while r:GetLine("end") do
local id = r:GetAsNumber()
local name = r:GetAsString()
end
r:Close()
end
Reader:Load opens the file with the standard Lua io.open function, so a relative path is interpreted relative to the working folder of the GameServer process (not the Data\LuaAPI folder). Use a full path, or use the File group functions (relative paths are counted from Data\LuaAPI) if you need to read a file in Data.