让 lua 脚本等待/暂停/睡眠/阻塞几秒钟的最简单方法? [英] Easiest way to make lua script wait/pause/sleep/block for a few seconds?
问题描述
我不知道如何让 lua 执行任何常见的计时技巧,例如
I cant figure out how to get lua to do any common timing tricks, such as
sleep - 停止线程上的所有操作
sleep - stop all action on thread
暂停/等待 - 不要继续下一个命令,但允许其他代码申请继续
pause/wait - don't go on to the next command, but allow other code in the application to continue
block - 不要继续执行下一个命令,直到当前返回
block - don't go on to next command until the current one returns
我读过那篇
while os.clock()<time_point do
--nothing
end
占用 CPU 时间.
有什么建议吗?是否有我遗漏的 API 调用?
Any suggestions? Is there an API call I'm missing?
更新:我很久以前写过这个问题,试图让 WOW Lua 按计划重播动作(即站立,等待 1 秒,跳舞,等待 2 秒,坐下.没有停顿,这些几乎都发生在同一季度.)事实证明,WOW 故意禁用了几乎所有允许在时钟上执行操作的功能,因为它可能会破坏游戏或启用机器人. 我想一旦它被拿走,就重新创建一个时钟,我必须做一些疯狂的事情,比如创建一个工作数组(带有一个动作和执行时间),然后在一堆常见事件上注册一个事件处理程序,比如鼠标移动,然后在偶数处理程序中,处理时机成熟的任何操作.事件处理程序实际上不会每 X 毫秒发生一次,但如果它每 2-100 毫秒发生一次,就足够接近了.遗憾的是我从未尝试过.
UPDATE: I wrote this question a long time ago trying to get WOW Lua to replay actions on a schedule (i.e. stand, wait 1 sec, dance, wait 2 sec, sit. Without pauses, these happen almost all in the same quarter second.) As it turned out WOW had purposely disabled pretty much everything that allows doing action on a clock because it could break the game or enable bots. I figured to re-create a clock once it had been taken away, I'd have to do something crazy like create a work array (with an action and execution time) and then register an event handler on a bunch of common events, like mouse move, then in the even handler, process any action whose time had come. The event handler wouldn't actually happen every X milliseconds, but if it was happening every 2-100 ms, it would be close enough. Sadly I never tried it.
推荐答案
没有比这更容易的了.睡眠可能会在您的 FLTK 或其他任何东西中实现,但这涵盖了在没有特殊事件中断的情况下进行标准系统睡眠的所有最佳方法.瞧:
It doesn't get easier than this. Sleep might be implemented in your FLTK or whatever, but this covers all the best ways to do standard sort of system sleeps without special event interrupts. Behold:
-- we "pcall" (try/catch) the "ex", which had better include os.sleep
-- it may be a part of the standard library in future Lua versions (past 5.2)
local ok,ex = pcall(require,"ex")
if ok then
-- print("Ex")
-- we need a hack now too? ex.install(), you say? okay
pcall(ex.install)
-- let's try something else. why not?
if ex.sleep and not os.sleep then os.sleep = ex.sleep end
end
if not os.sleep then
-- we make os.sleep
-- first by trying ffi, which is part of LuaJIT, which lets us write C code
local ok,ffi = pcall(require,"ffi")
if ok then
-- print("FFI")
-- we can use FFI
-- let's just check one more time to make sure we still don't have os.sleep
if not os.sleep then
-- okay, here is our custom C sleep code:
ffi.cdef[[
void Sleep(int ms);
int poll(struct pollfd *fds,unsigned long nfds,int timeout);
]]
if ffi.os == "Windows" then
os.sleep = function(sec)
ffi.C.Sleep(sec*1000)
end
else
os.sleep = function(sec)
ffi.C.poll(nil,0,sec*1000)
end
end
end
else
-- if we can't use FFI, we try LuaSocket, which is just called "socket"
-- I'm 99.99999999% sure of that
local ok,socket = pcall(require,"socket")
-- ...but I'm not 100% sure of that
if not ok then local ok,socket = pcall(require,"luasocket") end
-- so if we're really using socket...
if ok then
-- print("Socket")
-- we might as well confirm there still is no os.sleep
if not os.sleep then
-- our custom socket.select to os.sleep code:
os.sleep = function(sec)
socket.select(nil,nil,sec)
end
end
else
-- now we're going to test "alien"
local ok,alien = pcall(require,"alien")
if ok then
-- print("Alien")
-- beam me up...
if not os.sleep then
-- if we still don't have os.sleep, that is
-- now, I don't know what the hell the following code does
if alien.platform == "windows" then
kernel32 = alien.load("kernel32.dll")
local slep = kernel32.Sleep
slep:types{ret="void",abi="stdcall","uint"}
os.sleep = function(sec)
slep(sec*1000)
end
else
local pol = alien.default.poll
pol:types('struct', 'unsigned long', 'int')
os.sleep = function(sec)
pol(nil,0,sec*1000)
end
end
end
elseif package.config:match("^\") then
-- print("busywait")
-- if the computer is politically opposed to NIXon, we do the busywait
-- and shake it all about
os.sleep = function(sec)
local timr = os.time()
repeat until os.time() > timr + sec
end
else
-- print("NIX")
-- or we get NIXed
os.sleep = function(sec)
os.execute("sleep " .. sec)
end
end
end
end
end
这篇关于让 lua 脚本等待/暂停/睡眠/阻塞几秒钟的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!