【问题标题】:How to terminate Lua script?如何终止 Lua 脚本?
【发布时间】:2017-09-01 06:43:43
【问题描述】:

如何终止 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

【问题讨论】:

    标签: lua minecraft computercraft


    【解决方案1】:

    正如 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
    

    【讨论】:

    • error() 在这种情况下应该可以正常工作,但我还添加了更好的解决方案。
    • 谢谢,虽然这在 Lua 程序的函数调用中不起作用。 (我遇到了同样的问题。)
    • 在 computercraft "os.exit()" 中不会关闭父 lua 实例。在 CC 中“os.exit()”被重新映射到一个只会“关闭”计算机或 Turtle 的函数。
    【解决方案2】:

    break 语句将跳到它所在的forwhilerepeat 循环之后的行。

    while true do
        if turtle.detect() then
            if turtle.getItemCount(16) == 64 then
                break
            end
            turtle.dig() -- digs block in front of it
        end
    end
    -- break skips to here
    

    lua 的一个怪癖:break 必须出现在 end 之前,但不一定是您想要跳出的循环的 end,正如您在此处看到的那样。

    此外,如果您想在循环开始或结束的条件下退出循环,如上所述,通常您可以更改您正在使用的循环以获得类似的效果。例如,在本例中,我们可以将条件放在while 循环中:

    while turtle.getItemCount(16) < 64 do
      if turtle.detect() then
        turtle.dig()
      end
    end
    

    请注意,我在那里稍微改变了行为,因为这个新循环将在达到项目计数限制时立即停止,直到detect() 再次变为真。

    【讨论】:

      【解决方案3】:

      标准 Lua 中没有名为 exit 的全局函数。

      但是,有一个os.exit 函数。在 Lua 5.1 中,它有一个可选参数,即错误代码。在 Lua 5.2 上,有第二个可选参数,告诉 Lua 状态是否应该在退出之前关闭。

      但请注意,Minecraft ComputerCraft 可能提供与标准 os.exit 不同的功能。

      【讨论】:

      • os.exit() 函数不会退出 ComputerCraft 中的程序。如果你尝试运行它,你会得到一个错误。相反,请使用 shell.exit() computercraft.info/wiki/Shell.exit
      【解决方案4】:

      您也可以通过在海龟/计算机界面中按住 Ctrl + T 几秒钟来手动终止它。

      【讨论】:

        【解决方案5】:

        不要使用while true

        做这样的事情:

        running = true
        while running do
        
            -- dig block
                turtle.dig() --digs block in front of it
        
            -- check your condition and set "running" to false
            if turtle.getItemCount(16) == 64 then
                running = false
            end
        
        end
        

        此外,您不必在挖掘之前致电 turtle.detect(),因为 turtle.dig() 会在内部再次调用它

        【讨论】:

          【解决方案6】:

          不要使用while true。而不是它使用这样的东西:

          while turtle.getItemCount(16) < 64 do
            if turtle.detect() then
              turtle.dig()
            end
          end
          

          它会为你工作。

          【讨论】:

            【解决方案7】:

            shell.exit() 关闭计算机工艺中的 lua 脚本。 欲了解更多信息,请转到http://computercraft.info/wiki/Shell.exit

            【讨论】:

            • 它不会结束当前正在执行的脚本。所以这并不能回答问题。
            猜你喜欢
            • 1970-01-01
            • 2021-12-28
            • 2011-12-10
            • 1970-01-01
            • 2013-11-15
            • 2021-11-12
            • 2010-10-07
            • 1970-01-01
            • 2010-11-19
            相关资源
            最近更新 更多