Jump to content




Goto command or Terminate program

turtle lua

4 replies to this topic

#1 Doobliheim

  • Members
  • 6 posts
  • LocationPortland, OR

Posted 24 November 2015 - 09:57 PM

Within the computercraft Lua system, is there a way to jump to a certain line of code? I want to write an if/then/else statement where if the turtle has torches, it does a certain task, and if it doesn't it, it prints a message and then kills the entire program. I was thinking of using the goto command to just jump to the very end of the script and end it, but it says "bios:14: [string "Test"]:24: '=' expected" when it runs. Line 24 is where I wrote the "goto break" so it would jump to the line with ::break:: on it. Any ideas on how to fix this or on how to kill the program if the torch check fails?

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 24 November 2015 - 10:04 PM

You can use error() to terminate your program.

#3 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 25 November 2015 - 03:10 PM

Some advanced information about the use of error() to terminate programs.

error() can cause some problems if you are going to embed your program inside of another program, error() is meant to be used to tell the program which started your program (usually the shell, but can be others) that something has gone wrong which your program didn't expect and can't work around or fix.

The reason I am mentioning this is because error crashes not only your program, but also any program up the call stack until pcall() was used (the shell uses pcall() to run programs).

Unless your program has encountered an error, you should use return to exit. However return will only exit your program is used from outside of functions.

I would link some documentation for pcall() and error(), but I can't find any that is easy to read.

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 26 November 2015 - 12:33 AM

 Lupus590, on 25 November 2015 - 03:10 PM, said:

error() can cause some problems if you are going to embed your program inside of another program, error() is meant to be used to tell the program which started your program (usually the shell, but can be others) that something has gone wrong which your program didn't expect and can't work around or fix.

The reason I am mentioning this is because error crashes not only your program, but also any program up the call stack until pcall() was used (the shell uses pcall() to run programs).

One could argue that that's not the problem of the programmer who wants to call error(). If their script is being executed by another script, then it's up to that second script to handle the situation if an error occurs.

Within ComputerCraft, at least, you really need to go out of your way to turn this into an issue. os.run() / shell.run() each implement pcall() for you.

Don't get me wrong, return is certainly "neater"; but due to Lua 5.1's lack of goto, it's seldom convenient.

#5 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 28 November 2015 - 04:39 PM

Not sure how big code want to goto but you can nest loops to do this:
repeat
...
until true

In the (...) you can break to go down
To go up:
local a = true
repeat
repeat
...
until true
until a
To go up you just set a to false and then break.

This solution is not optimal, and it is hard to work with if you are using extra loops inside and you need an extra repeat loop for every place you want to go to, so use it when it's really neccessary and the block is short.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users