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

前端 未结 19 1361
礼貌的吻别
礼貌的吻别 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:40

    For the second request, pause/wait, where you stop processing in Lua and continue to run your application, you need coroutines. You end up with some C code like this following:

    Lthread=lua_newthread(L);
    luaL_loadfile(Lthread, file);
    while ((status=lua_resume(Lthread, 0) == LUA_YIELD) {
      /* do some C code here */
    }
    

    and in Lua, you have the following:

    function try_pause (func, param)
      local rc=func(param)
      while rc == false do
        coroutine.yield()
        rc=func(param)
      end
    end
    
    function is_data_ready (data)
      local rc=true
      -- check if data is ready, update rc to false if not ready
      return rc
    end
    
    try_pause(is_data_ready, data)
    

提交回复
热议问题