gpt4 book ai didi

lua - 让 lua 脚本等待/暂停/ sleep /阻塞几秒钟的最简单方法?

转载 作者:行者123 更新时间:2023-12-03 08:44:30 27 4
gpt4 key购买 nike

我无法弄清楚如何让 lua 执行任何常见的计时技巧,例如

  • sleep - 停止对线程的所有操作
  • 暂停/等待 - 不要继续下一个
    命令,但允许其他代码
    申请继续
  • 阻止 - 不要继续下一个命令,直到
    当前返回

  • 我读过一个
    while os.clock()<time_point do 
    --nothing
    end

    占用 CPU 时间。

    有什么建议?是否有我遗漏的 API 调用?

    更新 :我很久以前写过这个问题,试图让 WOW Lua 按计划重播 Action (即站立,等待 1 秒,跳舞,等待 2 秒,坐下。没有停顿,这些几乎都发生在同一四分之一秒内。) 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.我想在它被拿走后重新创建一个时钟,我必须做一些疯狂的事情,比如创建一个工作数组(带有一个 Action 和执行时间),然后在一堆常见事件上注册一个事件处理程序,比如鼠标移动,然后在偶数处理程序中,处理时机成熟的任何 Action 。事件处理程序实际上不会每 X 毫秒发生一次,但如果它每 2-100 毫秒发生一次,那就足够接近了。遗憾的是我从未尝试过。

    最佳答案

    没有比这更容易的了。 sleep 可能在您的 FLTK 或其他任何东西中实现,但这涵盖了在没有特殊事件中断的情况下执行标准系统 sleep 的所有最佳方法。看:

    -- 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 - 让 lua 脚本等待/暂停/ sleep /阻塞几秒钟的最简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1034334/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com