This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MegaMech's profile picture

[Lua] [Question] If Functions and Goto's

Started by MegaMech, 21 December 2012 - 05:40 AM
MegaMech #1
Posted 21 December 2012 - 06:40 AM
Now obviously Goto's are not implemented in Computercraft's Lua (I think)
I've come into this problem quite a bit while coding… Skipping a line or two of code, or going back to the start of a function.
I know that goto's are usually a bad idea, but I always have the "goto" stuck in my head for these situations.
(A quick fix, if not, a bad quick fix)

what i'm doing is making a prompt (something that's easy to do in batch)

are you sure? Y/N: read()
1 or 2 would be Y or N
if anything other then Y or N is inputted then restart probably saying print("Not applicable, I only accept Y or N")

(of course I would have something to change variable to 1 or 2)

Function name()
local variable = 0

if variable = 1 then do name() end --If the variable is 1 then run the function again. (perhaps another end, would end name()? Then restart name())
if variable = 2 then goto end --goto could be replaced by calling skip()
else do name() --If the variable == something other then 1, or 2 then restart name()
::end:: --(this is where goto go's, succesfully skipping else)
end
function skip() --skips rest of name()
end

name()

I'm pretty sure that you can't call functions at the end of a statement like what i'm doing. Lua just ignores it

if variable = 1 then do name() end --program ignores name() and just continues running name() from where it is.

So my question is, what should you do in this situation? What's the best way to successfully restart the function, and what's the best way to skip a few lines of code if the variable is correct.

If you need more information or something sounds confusing, please tell me so I may correct it.
Lyqyd #2
Posted 21 December 2012 - 06:44 AM
Maybe a simple loop?


local input
repeat
  print("Y/N?")
  input = read()
until input == "Y" or input == "N"
Orwell #3
Posted 21 December 2012 - 07:01 AM
Lyqyd's solution is certainly the way to go (adapt it to your needs). But if you want to know more about goto's in Lua, check their wiki:
http://lua-users.org/wiki/GotoStatement
ChunLing #4
Posted 21 December 2012 - 07:15 AM
To "skip the rest" of a function, use return (usually with a return value). To exit the innermost current loop, use break (or include an or in your loop exit condition, if you're going to test for the break condition every time).

You can recurse functions, but generally this should be avoided if at all feasible. Recursion is a powerful but potentially costly tool.
MegaMech #5
Posted 21 December 2012 - 01:16 PM
Thanks for the help… I should be able to get something going with this…
MegaMech #6
Posted 21 December 2012 - 03:51 PM
Yup you guys were right, that's the way to do it.
The code is so much smaller it's not even funny…
finished result:


Function name()
repeat
input = read()
until input == "Y"

end
name()

thanks
ChunLing #7
Posted 21 December 2012 - 05:16 PM
…if that's all your doing, you can just use
repeat until read() == "Y"
But that seems kinda annoying "guess what input you need to enter until you get it right". The informative output seems necessary to avoid that.
MegaMech #8
Posted 23 December 2012 - 03:26 PM
Well of course I have
write("blah blah blah Y/N:")
ChunLing #9
Posted 23 December 2012 - 04:03 PM
That's good then.