How to add “sleep” or “wait” to my Lua Script?

I am trying to make a simple script for the game by changing the time of day, but I want to make it a quick move. So this is what I'm talking about:

function disco ( hour, minute) setTime ( 1, 0 ) SLEEP setTime ( 2, 0 ) SLEEP setTime ( 3, 0 ) end 

etc. How can i do this?

+6
source share
4 answers

Lua does not provide a standard sleep function, but there are several ways to implement it, see Sleep Function for details.

For Linux, this might be the easiest:

 function sleep(n) os.execute("sleep " .. tonumber(n)) end 

On Windows, you can use ping :

 function sleep(n) if n > 0 then os.execute("ping -n " .. tonumber(n+1) .. " localhost > NUL") end end 

Anyone who uses select deserves some attention because this is the only portable way to get sub-seasonal resolution:

 require "socket" function sleep(sec) socket.select(nil, nil, sec) end sleep(0.2) 
+12
source

If you have luasocket installed:

 local socket = require 'socket' socket.sleep(0.2) 
+2
source

wxLua has three sleep functions:

 local wx = require 'wx' wx.wxSleep(12) -- sleeps for 12 seconds wx.wxMilliSleep(1200) -- sleeps for 1200 milliseconds wx.wxMicroSleep(1200) -- sleeps for 1200 microseconds (if the system supports such resolution) 
+1
source

This homegrown feature has accuracy down to the 10th second or less.

 function sleep (a) local sec = tonumber(os.clock() + a); while (os.clock() < sec) do end end 
+1
source

Source: https://habr.com/ru/post/950794/


All Articles