6 posts
Location
Portland, 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?
8543 posts
Posted 24 November 2015 - 10:04 PM
You can use error() to terminate your program.
2427 posts
Location
UK
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.
7083 posts
Location
Tasmania (AU)
Posted 26 November 2015 - 12:33 AM
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.
779 posts
Location
Kerbin
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.