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:

  1. System\Constants
  2. System\FileReader
  3. System\Modules
  4. System\Callbacks
  5. System\CallbacksDB
  6. System\Timers
  7. System\Library
  8. The modules via the LoadModule function (in the order of the lines in Main.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.lua
  • Data\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:

  1. Enable: remove the -- at the start of the module's LoadModule line.
  2. Disable: add -- at the start of the line, or set Enable = false in the module's Config.lua (most modules have this key).
  3. 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

  1. Create the folder Data\LuaAPI\Includes\NewName\ with the file NewName.lua (and Config.lua if needed).
  2. In that file, write the handler functions and attach them to events with BridgeFunctionAttach (see Attaching a module to events).
  3. Add the line LoadModule("NewName\\NewName") to Main.lua.
  4. 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 path a/b). Empty lines or lines with only a comment are skipped.
  • GetLine(end_text) returns false at the end of the file or when it meets a line starting with end_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 for nil before calculating).
  • Returns nil if the file cannot be opened. Call Close() 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.