13

主题

25

回帖

991

积分

优秀创作者

积分
991

夏日·闪闪发光勋章

发表于  2023-7-10 14:48:13 | 显示全部楼层 |阅读模式 1050 2

_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


13

主题

25

回帖

991

积分

优秀创作者

积分
991

夏日·闪闪发光勋章

发表于  2023-7-10 14:49:34 | 显示全部楼层
lua的coroutine和y3自带的timer有冲突,不要嵌套使用。此外这里只提供思路,对于项目落地不做保证。

53

主题

102

回帖

2367

积分

精英创作者

积分
2367

夏日·活跃之王勋章

发表于  2023-7-20 09:56:41 | 显示全部楼层
666666666666666666666666666