如何终止Lua脚本?
发布时间:2020-12-15 00:23:58 所属栏目:大数据 来源:网络整理
导读:我如何终止Lua脚本?现在我有退出()的问题,我不知道为什么. (这是更多的Minecraft ComputerCraft问题,因为它使用的API包括.)这是我的代码: while true do if turtle.detect() then if turtle.getItemCount(16) == 64 then exit() --here is where I get pro
我如何终止Lua脚本?现在我有退出()的问题,我不知道为什么. (这是更多的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实例.
在Minecraft ComputerCraft中,调用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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |