本页包含 GameServer 剩下的两个函数(LoadScript 和 Event.Refresh),以及关于 Data\LuaAPI\System\Modules.lua 中简称的注意事项。
其他函数
LoadScript
加载并运行一个 Lua 文件。该函数是全局函数,不属于任何表。
LoadScript(path)
| 参数 | 类型 | 说明 |
|---|---|---|
path | string | .lua 文件路径,遵循与 File.* 函数相同的路径规则:相对路径从 Data\LuaAPI\ 开始计算 |
返回值:没有返回值。如果无法打开文件,或文件有语法错误 / 运行时错误,函数会向调用它的脚本抛出 Lua 错误(错误行会写明文件名)。会去掉文件开头的 UTF-8 BOM,并且如果第一行以 # 开头则去掉该行。支持含有非 ASCII 字符的文件名。
pcall 包裹调用:local ok, err = pcall(LoadScript, "Includes\\MyModule\\Extra.lua")。LoadScript("Includes\\MyModule\\Config.lua")
LoadScript("Includes\\MyModule\\MyModule.lua")
Event.Refresh
通知 GameServer 重新扫描脚本已定义的回调函数(onPlayerLogin、onItemUse...),使 GameServer 开始发送这些事件。GameServer 只向已存在的回调发送事件,因此没有人监听的事件不会消耗资源。
Event.Refresh()
无参数,无返回值。Modules.lua 中的 BridgeFunctionAttach 函数在把模块的函数挂接到新事件时会自动调用此函数,因此通常模块不需要直接调用。只有在脚本加载完成之后,你自己又定义了某个 onXxx 回调时才需要调用。
function onPlayerLevelUp(aIndex)
Message.Send(aIndex, 1, "恭喜升级!")
end
Event.Refresh()
具有特殊行为的简称
Modules.lua 中的大多数简称只是命名空间函数的别名,已在各函数处注明。有些简称是外层的 Lua 包装函数,行为有所不同:
| 简称 | 行为 |
|---|---|
GetObjectMasterReset(aIndex) | 始终返回 0(服务器没有 Master Reset 计数器) |
GetObjectLang(aIndex) | 始终返回 0(单一语言) |
GetGameServerLang()、GetGameServerProtocol() | 始终返回 0 |
Is28Option() | 始终返回 1 |
RandomGetNumber(n)、GetRandomValue(n) | 从 0 到 n - 1 的随机数(与包含两端的 Util.Random 不同) |
CommandCheckGameMasterLevel(aIndex, level) | 只检查是否为已开启权限的 GM;忽略 level |
GetObjectGensFamily(aIndex) | 两个阵营与 Player.GetGensFamily 相反 |
InventoryGetItemTable、EventInventoryGetItemTable、MuunInventoryGetItemTable、MapGetItemTable | 返回的物品表额外包含旧式字段名 |
InventorySetItemTable(aIndex, slot, t) | 同时接受旧式字段名 |
尚未支持的简称
下列名称在 Modules.lua 中仅作占位,尚无功能。调用它们会引发 Lua 错误,内容为 <名称>() is not supported by LuaAPI(该错误只会中止调用它的函数,其他模块仍继续运行):
GetObjectMaxNormalPoints、GetObjectPassword、GetObjectPersonalCode、EventInventorySetItemTable、EventInventoryDelItemIndex、EventInventoryDelItemCount、MuunInventorySetItemTable、MuunInventoryDelItemCount、AddItemBag、PartyCreate、PartyDelete、PartyAddMember、PartyDelMember、SendRgbChat。