作用

在游戏中输入 /test 5 hello,玩家会收到一行 Hello <名称>! number = 5, text = hello。该模块只是一个示例,供您复制后制作自己的命令。

启用方法

  1. 命令已在 Data\Commands\LuaCommands.xml 中预先声明:<Command ID="2000" name="test" Description="" Authority="0"/>。
  2. Main.lua 中的 LoadModule("CmdCommands\\Commands") 这一行(默认已启用)。
  3. 点击 重新加载 LuaAPI。

该模块没有 Config.lua,也不需要 SQL。代码文件是 Includes\CmdCommands\Commands.lua,将函数 Commands_OnCommandManager 挂接到 OnCommandManager。

添加新命令

  1. 将 <Command ID="..." name="..." Authority="0"/> 这一行添加到 LuaCommands.xml。Authority = 0 表示所有人可用;非 0 则仅限已开启权限的管理员账号。请选择尚未被任何模块使用的 ID。
  2. 在通过 BridgeFunctionAttach("OnCommandManager", ...) 挂接的函数中处理该 ID。当命令属于自己时函数返回 1,否则返回 0。
  3. 点击 重新加载 LuaAPI。
local CMD_TEST = 2000

function Commands_OnCommandManager(aIndex, cmdtype, code, arg)
	if code ~= CMD_TEST then
		return 0
	end

	local number = math.floor(Util.ArgNumber(arg, 0))
	local text = Util.ArgString(arg, 1)

	Message.Send(aIndex, 1, string.format("Hello %s! number = %d, text = %s", Player.GetName(aIndex), number, text))
	return 1
end

BridgeFunctionAttach("OnCommandManager", "Commands_OnCommandManager")
如果某个命令存在于 LuaCommands.xml 中,但与服务器已有命令同名,则会执行服务器已有的命令,不会进入 Lua。参见 事件。