如何终止 Lua 脚本?现在我在 exit() 方面遇到问题,我不知道为什么。 (这更像是一个 Minecraft ComputerCraft 问题,因为它使用了包含的 API。)这是我的代码:
while true do
if turtle.detect() then
if turtle.getItemCount(16) == 64 then
exit() --here is where I get problems
end
turtle.dig() --digs block in front of it
end
end
正如prapin的回答所述,在Lua中该函数os.exit([code])
将终止主机程序的执行。然而,这可能不是您正在寻找的,因为调用os.exit
不仅会终止你的脚本,还会终止正在运行的父 Lua 实例。
In 我的世界 电脑工艺, 呼叫error()
也将完成您正在寻找的内容,但是将其用于其他目的而不是在发生错误后真正终止脚本可能不是一个好的做法。
因为在 Lua 中,所有脚本文件也被视为具有自己作用域的函数,所以退出脚本的首选方法是使用return
关键字,就像从函数返回一样。
像这样:
while true do
if turtle.detect() then
if turtle.getItemCount(16) == 64 then
return -- exit from the script and return to the caller
end
turtle.dig() --digs block in front of it
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)