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)
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
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.
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.