二.事件添加add和push执行
2.1.注册事件
D:\MirServer-lua-study\Mir200\Envir\QuestDiary\game\Global.lua
-------------------------------监听事件---------------------------------------
GameEvent.add(EventCfg.onRecharge, Global.Recharge, Global, 1)
-------------------------------监听网络---------------------------------------
Message.RegisterNetMsg(ssrNetMsgCfg.Global, Global)
2.2.事件类中注册和push
D:\MirServer-lua-study\Mir200\Envir\QuestDiary\util\GameEvent.lua
GameEvent = {}
local _listeners = {}
--GameEvent.add(EventCfg.onClicknpc, Global.ClickNpcResponse, Global, 1)
function GameEvent.add(eventName, func, tag, priority)
assert(tag, "Tag must not be nil")
if not _listeners[eventName] then
_listeners[eventName] = {}
end
local eventListeners = _listeners[eventName]
local eventListenerSize = #eventListeners
for i = 1, eventListenerSize do
if tag == eventListeners[i][2] then
return
end
end
if priority and eventListenerSize >= priority then
table.insert(eventListeners, priority, {func, tag})
else
table.insert(eventListeners, {func, tag})
end
end
--派发--GameEvent.push("onClicknpc", actor, npcid, npcobj)
--table.insert(eventListeners, {func, tag})
function GameEvent.push(eventName, ...)
--local eventListeners = _listeners["onClicknpc"]
local eventListeners = _listeners[eventName]
if not eventListeners then
return
end
for index, listeners in ipairs(eventListeners) do
pcall(Global.ClickNpcResponse())
local result, stop = pcall(listeners[1], ...)
release_print('-->result=',listeners[1])
if result then
if stop then break end
else
local tag = listeners[2]
local tarid = tag.ID or 0
local err = "派发事件发生错误:事件名="..eventName.." 模块ID="..tarid.." "
release_print(err, stop)
end
end
end
return GameEvent