如何添加“睡眠"状态或“等待"到我的Lua脚本? [英] How to add a "sleep" or "wait" to my Lua Script?

查看:130
本文介绍了如何添加“睡眠"状态或“等待"到我的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

,依此类推.我将如何去做?

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

推荐答案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆