【发布时间】:2020-08-31 18:56:37
【问题描述】:
这个函数一直有效,直到我把它放在一个 while true do 循环中。如果我把它放在循环中,它将无限等待。
编辑:我发现等待确实有效;然而,出于某种原因,即使它被包裹在协程中,它也会停止主线程。不知道为什么?
function wait(seconds)
local start = os.time()
repeat until os.time() > start + seconds
end
local function countDown()
while true do
wait(1)
if isInNumberGame == true then
timeSinceLastMessage = timeSinceLastMessage - 1
if timeSinceLastMessage == 0 then
isInNumberGame = false
local messageChannel = mem.guild:getChannel("668605956426563626")
messageChannel:send("<@"..currentmember.user.id.."> Game over! Out of time to respond (the number was "..num..")")
end
end
end
end
local countDownNumGame = coroutine.wrap(countDown)
countDownNumGame()
【问题讨论】:
-
您能否提供更多关于您正在使用的库或平台的信息?
-
我正在使用 discordia 作为我的库。 Roblox Lua 是我所经历的,但你说 Lua 是一种单线程语言。我希望 while true do 循环不会中断其余代码,而只是在后台运行。我认为这是协程的使用。不是吗?
-
如果没有,我应该使用什么,它不会暂停线程?
标签: lua