如何添加“睡眠"或“等待"到我的 Lua 脚本? [英] How to add a "sleep" or "wait" to my Lua Script?
本文介绍了如何添加“睡眠"或“等待"到我的 Lua 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过更改一天中的时间来为游戏制作一个简单的脚本,但我想快速完成.所以这就是我要说的:
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
等等.我该怎么做?
推荐答案
Lua 没有提供标准的 sleep
功能,但是有多种实现方式,见 睡眠功能了解详情.
Lua doesn't provide a standard sleep
function, but there are several ways to implement one, see Sleep Function for detail.
对于 Linux,这可能是最简单的:
For Linux, this may be the easiest one:
function sleep(n)
os.execute("sleep " .. tonumber(n))
end
在Windows中,您可以使用ping
:
In Windows, you can use ping
:
function sleep(n)
if n > 0 then os.execute("ping -n " .. tonumber(n+1) .. " localhost > NUL") end
end
使用 select
的方法值得关注,因为它是获得亚秒级分辨率的唯一可移植方式:
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)
这篇关于如何添加“睡眠"或“等待"到我的 Lua 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文