28

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

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

and so on. How would I go about doing this?

2

13 Answers 13

36

Lua doesn't provide a standard sleep function, but there are several ways to implement one, see Sleep Function for detail.

For Linux, this may be the easiest one:

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

In Windows, you can use ping:

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

The one using select deserves some attention because it is the only portable way to get sub-second resolution:

require "socket"

function sleep(sec)
    socket.select(nil, nil, sec)
end

sleep(0.2)
5
  • @DonavonDecker Follow the other approaches in the link then. There are several available for windows. For example, writing a C extension
    – Yu Hao
    Aug 1, 2013 at 7:41
  • 1
    I understand, but when I try the other method in the link , I get an error saying "attempt to index global 'os' nil value" Aug 1, 2013 at 7:44
  • @DonavonDecker That's odd, because os is a standard Lua library and I tested the one using ping under Windows XP myself, and it works fine. What version of Lua are you using?
    – Yu Hao
    Aug 1, 2013 at 7:49
  • 2
    I think he is using a game engine which doesn't provide the os library. If you are, please specify which one, as it is more likely to provide a better solution. Aug 8, 2013 at 21:33
  • Is this a good solution for a game that's meant to be played on an Android mobile device? Nov 27, 2018 at 13:31
8

If you have luasocket installed:

local socket = require 'socket'
socket.sleep(0.2)
1
  • 2
    How to install lua package? Feb 8, 2018 at 12:26
5

This homebrew function have precision down to a 10th of a second or less.

function sleep (a) 
    local sec = tonumber(os.clock() + a); 
    while (os.clock() < sec) do 
    end 
end
2
  • 6
    don't forget it is a busy wait, consuming processor time Jan 21, 2017 at 6:51
  • If you want a no busy wait for Lua for use in frames for videogames or applications, perhaps wxLua or other libraries may provide less resource craving sleep functions. The sleep function I provide is useful for bugtesting, automation in smaller scripts and less serious projects.
    – Mossarelli
    Jan 25, 2017 at 10:39
4

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)
2

I know this is a super old question, but I stumbled upon it while I was working on something. Here's some code that's working for me...

time=os.time()
wait=5
newtime=time+wait
while (time<newtime)
do
  time=os.time()
end

And I needed randomization so I added

math.randomseed(os.time())
math.random(); math.random(); math.random()
randwait = math.random(1,30)
time=os.time()
newtime=time+randwait
while (time<newtime)
do
  time=os.time()
end
1
  • 1
    That's a ‘busy wait’.
    – aaa
    Aug 3, 2023 at 20:34
1

I needed something simple for a polling script, so I tried the os.execute option from Yu Hao's answer. But at least on my machine, I could no longer terminate the script with Ctrl+C. So I tried a very similar function using io.popen instead, and this one does allow early termination.

function wait (s)
    local timer = io.popen("sleep " .. s)
    timer:close()
end
1

You should read this: http://lua-users.org/wiki/SleepFunction

There are several solutions and each one has a description, which is important to know.

This is, what I used:

function util.Sleep(s)
    if type(s) ~= "number" then
        error("Unable to wait if parameter 'seconds' isn't a number: " .. type(s))
    end
    -- http://lua-users.org/wiki/SleepFunction
    local ntime = os.clock() + s/10
    repeat until os.clock() > ntime
end
2
  • That's a ‘busy wait’.
    – aaa
    Aug 3, 2023 at 20:35
  • As I mentioned below, either unistd or posix.time provide direct access to POSIX' sleep function, on Unix systems. Idk about Windows.
    – aaa
    Aug 22, 2023 at 12:49
0

if you're using a MacBook or UNIX based system, use this:

function wait(time)
if tonumber(time) ~= nil then
os.execute("Sleep "..tonumber(time))
else
os.execute("Sleep "..tonumber("0.1"))
end
wait()
1
  • 1
    Files, and thus commands, are case-sensitive on most Unix systems, except for filesystems in MacOS by default. I.e. you need sleep instead of Sleep, for most OSes.
    – aaa
    Aug 3, 2023 at 20:38
0

You can use "os.time" or "os.clock" with "while" loop, i prefer "repeat until" loop because its shorter, but they are expensive because they cost full usage of a single core.

If you need something less demanding, you can use various wrappers like wxLua that i use, but sometimes, some of them also got usage penalty, specially annoying in games, so its best to test them and get what is best for your project.

Or you can relay on OS like Windows to do sleep function, using applications that exist in system32, via Batch or PowerShell, using ">nul" to hide it with "os.execute" or "io.popen", like "ping" (localhost/127.0.0.1) with timeout, "choice" (works with XP, newer versions may be different, i prefer it), "timeout" (/nobreak may be useless because all Windows commands can be canceled with CTRL+C). Downside are limited to given OS and number limitation as well as seconds or miliseconds, running it on eg. Linux may need Wine emulation for Windows (if application are written for it). You can also use "sleep" or "start-sleep" (from PowerShell), but since Lua are standalone, most people prefer pure Lua or wrappers, and you can use what suits your project.

0

The safe function sleep (n) for Lua:

function sleep (n)
    while n > 0 do
        print("Sleeping for ".. tonumber(n) .." seconds...")
        os.execute("ping -n 2 localhost > nul")
        n = n-1
    end
end
0

Luaposix' unixstd module provides direct access to the sleep function on POSIX systems, i.e. Linux, MacOS and such.

local unistd = require('posix.unistd')
unistd.sleep(5)

sleep only has a second resolution, but posix.time.nanosleep apparently has nanosecond precision:

local time = require('posix.time')
time.nanosleep({tv_sec=5, tv_nsec=500})
0
local function wait(ms)
   local start = os.clock();
   repeat until (os.clock() - start) > (ms / 1000)
end
1
  • Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation? Apr 7 at 0:10
-3
function wait(time)
    local duration = os.time() + time
    while os.time() < duration do end
end

This is probably one of the easiest ways to add a wait/sleep function to your script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.