Lua 正确重新执行协同程序?
我正在使用lua创建一个游戏;我需要一个计时器在比赛间歇和比赛轮次期间运行。游戏开始时暂停15秒。在本例中,计时器运行正常,但对函数的后续调用似乎根本不会触发它……有什么提示吗 我已尝试将我的协同程序创建方法替换为coroutine.wrap(),而不是coroutine.create()。 当在第一次调用后成功运行时,协同路由的状态仍然显示为让步 此处显示的简化逻辑:其中秒是整数,结尾是布尔值Lua 正确重新执行协同程序?,lua,coroutine,Lua,Coroutine,我正在使用lua创建一个游戏;我需要一个计时器在比赛间歇和比赛轮次期间运行。游戏开始时暂停15秒。在本例中,计时器运行正常,但对函数的后续调用似乎根本不会触发它……有什么提示吗 我已尝试将我的协同程序创建方法替换为coroutine.wrap(),而不是coroutine.create()。 当在第一次调用后成功运行时,协同路由的状态仍然显示为让步 此处显示的简化逻辑:其中秒是整数,结尾是布尔值 module.startTimer = coroutine.create(function(seco
module.startTimer = coroutine.create(function(seconds,ending)
wait()
print("Timer starting with: "..seconds.." seconds...round ending: "..tostring(ending))
while seconds > -1 do
wait(1)
seconds = seconds - 1
end
if ending == true then
coroutine.yield(module.startTimer)
else
coroutine.yield(module.startTimer)
end
end)
第一次呼叫(工作):
打印:暂停,暂停
第二个调用(不起作用):其中RoundLength.Value是一个可验证的int值(300),可以成功打印
print(coroutine.status(module.startTimer))
coroutine.resume(module.startTimer,CURRENT_ROUND:FindFirstChild("RoundLength").Value,true)
wait()
print(coroutine.status(module.startTimer))
wait(CURRENT_ROUND:FindFirstChild("RoundLength").Value)
不打印任何内容,不执行,startTimer不打印状态。我想你可能误解了Lua的协同程序(它们不同于正常的协同程序,因为它们是不对称的),尽管我不能确定 如前所述,函数将循环直到时间结束,然后产生自身(?),这样它自己的函数值将从
coroutine.resume
调用返回
一旦恢复,它将再次从coutroutine.yield
调用开始,到达函数末尾,然后返回,结束协同例程的执行
print(coroutine.status(module.startTimer))
coroutine.resume(module.startTimer,CURRENT_ROUND:FindFirstChild("RoundLength").Value,true)
wait()
print(coroutine.status(module.startTimer))
wait(CURRENT_ROUND:FindFirstChild("RoundLength").Value)