문제

How would I terminate a Lua script? Right now I'm having problems with exit(), and I don't know why. (This is more of a Minecraft ComputerCraft question, since it uses the APIs included.) Here is my code:

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
도움이 되었습니까?

해결책

As prapin's answer states, in Lua the function os.exit([code]) will terminate the execution of the host program. This, however, may not be what you're looking for, because calling os.exit will terminate not only your script, but also the parent Lua instances that are running.

In Minecraft ComputerCraft, calling error() will also accomplish what you're looking for, but using it for other purposes than genuinely terminating the script after an error has occurred is probably not a good practice.

Because in Lua all script files are also considered functions having their own scope, the preferred way to exit your script would be to use the return keyword, just like you return from functions.

Like this:

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

다른 팁

The break statement will skip to the line after the for, while, or repeat loop it's in.

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

A quirk of lua: break has to come just before an end, though not necessarily the end of the loop you want to break out of, as you can see here.

Also, if you want to exit the loop on a condition at the start or end of the loop, as above, often you can change up the loop you're using to get a similar effect. For instance, in this example we could put the condition in the while loop:

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

Note that I subtly changed the behaviour a bit there, as this new loop will stop right away when it hits the item count limit, without continuing until detect() becomes true again.

There is no global function named exit in standard Lua.

However, there is an os.exit function. In Lua 5.1, it has one optional argument, the error code. On Lua 5.2, there is a second optional parameter, telling whether the Lua state should be closed before exiting.

But note that Minecraft ComputerCraft may provide a different function than the standard os.exit one.

You can also terminate it manually by holding down Ctrl + T a few seconds in the turtle/computer's interface.

don't use a while true

Do something like this:

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

Also you don't have to call turtle.detect() before digging 'cause turtle.dig() wil call it again internal

Don't use while true. Instead of it use something like this:

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

It will work for you.

shell.exit() closes a lua script in computer craft. for further info go to http://computercraft.info/wiki/Shell.exit

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top