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

Jump in code

Started by nikolay04, 02 February 2017 - 04:27 PM
nikolay04 #1
Posted 02 February 2017 - 05:27 PM
Is there any way to jump in the code . Like in C# you can do someting like that

top:
blablabla
blabl
bbt
bla
test
notimportant
goto top:
Dog #2
Posted 02 February 2017 - 05:33 PM
Newer versions of Lua support a goto command, but CC Lua does not. I believe the only method of 'jumping around' is to use functions and call them like so…

local function one()
  --# do something
end

local function two()
  --# do something
end

local function three()
  if not x then
    two()
  else
    one()
  end
end
nikolay04 #3
Posted 02 February 2017 - 05:38 PM
Thanks, didn't think about functions :)/>
Bomb Bloke #4
Posted 03 February 2017 - 12:02 AM
Though, if you want to create a loop structure then you should use a loop keyword, such as "while".

http://lua-users.org/wiki/ControlStructureTutorial

If you attempt to repeat a section of code by calling functions over and over then odds are you'll crash out with a stack overflow. Functions which call functions stay in memory, see, and if they're calling themselves recursively then you'll run out of the stuff pretty quickly.

About the one time you'd want to use "goto" is to escape from the middle of a nested loop structure (deep enough that you can't "break" the whole lot in one go). In that case, since Lua 5.1 doesn't have "goto", you might instead bundle the whole lot off into a function, which you could then "return" from at any point.