Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a "sleep" or "wait" to my Lua Script?

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?

like image 502
Donavon Decker Avatar asked Aug 01 '13 07:08

Donavon Decker


2 Answers

If you have luasocket installed:

local socket = require 'socket'
socket.sleep(0.2)
like image 32
konrad Avatar answered Sep 24 '22 18:09

konrad


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)
like image 116
Yu Hao Avatar answered Sep 24 '22 18:09

Yu Hao