Jump to content




Jump in code


3 replies to this topic

#1 nikolay04

  • Members
  • 9 posts

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:


#2 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

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


#3 nikolay04

  • Members
  • 9 posts

Posted 02 February 2017 - 05:38 PM

Thanks, didn't think about functions :)

#4 Bomb Bloke

    Hobbyist Coder

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

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

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users