EGNE#6985 发表于 2023-7-10 14:48:13

一种事件机制的实现思路


_gUdefCoroutinePool = {} -- 自定义线程池
-- 自定义唤醒事件
function udefDispatchEvent(event, ...)
    local parameterList = { ... }
    local co = coroutine.create(function()
      while true do
            coroutine.yield(event, parameterList)
      end
    end)
    table.insert(_gUdefCoroutinePool, { ["co"] = co, ["ev"] = event })
end

-- 自定义注册事件
function udefRegisterEvent(event, func)
    for _, v in pairs(_gUdefCoroutinePool) do
      local bool, _event, parameterList = coroutine.resume(v.co)
      if event == _event then
            func(unpack(parameterList))
            return v
      end
    end
end

-- 取消唤醒事件
function removeUdefDispatchEvent(event)
    for k, v in pairs(_gUdefCoroutinePool) do
      if event == v.ev then
            table.remove(-_gUdefCoroutinePool, k)
            break
      end
    end
end


EGNE#6985 发表于 2023-7-10 14:49:34

lua的coroutine和y3自带的timer有冲突,不要嵌套使用。此外这里只提供思路,对于项目落地不做保证。

喵头嘤#4895 发表于 2023-7-20 09:56:41

666666666666666666666666666
页: [1]
查看完整版本: 一种事件机制的实现思路