使用yield线程的函数影响另一个函数的运行

我创建了这个函数在CoreGames中工作

-- 等待直到条件返回true,每隔 'interval' 秒钟检查一次
function _G.WaitUntil(condition, interval)
    if not interval then
        interval = 1/60
    end
    local tread = coroutine.running()
    if tread then
        local function check()
            if condition() then
                coroutine.resume(tread)
            else
                Task.Spawn(check, interval)
            end
        end
        Task.Spawn(check, interval)
        coroutine.yield(tread)
    else
        error("失败", 2)
    end
end

它有效地使一个坦克的生成延迟到比赛重新开始,但不能控制一个回合的开始,因为我想要控制 Game.StartRound() 函数的执行,但是它不能成功运行,线程停在了那里 我认为它失败的原因是因为我在第一个例子中将其添加到了一个 EventListener 中,在第二个例子中将其添加到了主代码中, 第一个例子:

local function boolexpr()
    return _G.ActualMision == 1
end

Game.roundEndEvent:Connect(function ()
    -- 等待直到实际任务为1
    if coroutine.isyieldable() then
        print("恢复坦克")
    end
    _G.WaitUntil(boolexpr, 0.016)
    -- 在恢复的同时
    Actual:Die()
end)

第二个例子:

Game.playerJoinedEvent:Connect(function (player)
    if _G.IsInRound then
        Task.Wait(1)
        Events.BroadcastToAllPlayers("显示选择 UI", player)
    end
end)

_G.WaitUntil(function () return #Game.GetPlayers() > 0 end, 1)

Task.Wait(5)

Game.StartRound()

这对什么有影响吗?

原文链接 https://stackoverflow.com/questions/69681884

点赞