Easiest way to make lua script wait/pause/sleep/block for a few seconds?

前端 未结 19 1237
礼貌的吻别
礼貌的吻别 2020-12-03 00:41

I cant figure out how to get lua to do any common timing tricks, such as

  • sleep - stop all action on thread

  • pause/wait - don\'t go on to the

相关标签:
19条回答
  • 2020-12-03 01:16

    I believe for windows you may use: os.execute("ping 1.1.1.1 /n 1 /w <time in milliseconds> >nul as a simple timer. (remove the "<>" when inserting the time in milliseconds) (there is a space between the rest of the code and >nul)

    0 讨论(0)
  • 2020-12-03 01:17

    [I was going to post this as a comment on John Cromartie's post, but didn't realize you couldn't use formatting in a comment.]

    I agree. Dropping it to a shell with os.execute() will definitely work but in general making shell calls is expensive. Wrapping some C code will be much quicker at run-time. In C/C++ on a Linux system, you could use:

    static int lua_sleep(lua_State *L)
    {
        int m = static_cast<int> (luaL_checknumber(L,1));
        usleep(m * 1000); 
        // usleep takes microseconds. This converts the parameter to milliseconds. 
        // Change this as necessary. 
        // Alternatively, use 'sleep()' to treat the parameter as whole seconds. 
        return 0;
    }
    

    Then, in main, do:

    lua_pushcfunction(L, lua_sleep);
    lua_setglobal(L, "sleep");
    

    where "L" is your lua_State. Then, in your Lua script called from C/C++, you can use your function by calling:

    sleep(1000) -- Sleeps for one second
    
    0 讨论(0)
  • 2020-12-03 01:17
    require 'alien'
    
    if alien.platform == "windows" then
      kernel32 = alien.load("kernel32.dll")
      sleep = kernel32.Sleep
      sleep:types{ret="void",abi="stdcall","uint"}
    else
      -- untested !!!
      libc = alien.default
      local usleep = libc.usleep
      usleep:types('int', 'uint')
      sleep = function(ms)
        while ms > 1000 do
          usleep(1000)
          ms = ms - 1000
        end
        usleep(1000 * ms)
      end
    end 
    
    print('hello')
    sleep(500)  -- sleep 500 ms
    print('world')
    
    0 讨论(0)
  • 2020-12-03 01:18
    cy = function()
        local T = os.time()
            coroutine.yield(coroutine.resume(coroutine.create(function()
        end)))
        return os.time()-T
    end
    sleep = function(time)
        if not time or time == 0 then 
            time = cy()
        end
        local t = 0
        repeat
            local T = os.time()
            coroutine.yield(coroutine.resume(coroutine.create(function() end)))
            t = t + (os.time()-T)
        until t >= time
    end
    
    0 讨论(0)
  • 2020-12-03 01:19

    You want  win.Sleep(milliseconds), methinks.

    Yeah, you definitely don't want to do a busy-wait like you describe.

    0 讨论(0)
  • 2020-12-03 01:20

    If you happen to use LuaSocket in your project, or just have it installed and don't mind to use it, you can use the socket.sleep(time) function which sleeps for a given amount of time (in seconds).

    This works both on Windows and Unix, and you do not have to compile additional modules.

    I should add that the function supports fractional seconds as a parameter, i.e. socket.sleep(0.5) will sleep half a second. It uses Sleep() on Windows and nanosleep() elsewhere, so you may have issues with Windows accuracy when time gets too low.

    0 讨论(0)
提交回复
热议问题