FANDOM


(fixed small typo)
(Adding categories)
Line 28: Line 28:
 
print 'C'
 
print 'C'
 
if math.random() < 0.1 then goto a else goto b end
 
if math.random() < 0.1 then goto a else goto b end
  +
[[Category:Lua]]

Revision as of 07:21, February 24, 2020

The goto system is related to goto in the C language (and many others) and will be familiar to those with experience outside Lua.

How to use Goto

Goto allows the user to define code blocks with Labels and the jump to them or "goto" them, at any point.

A label is defined with two colons (::) on either side of it's name.

::a_label::



To jump to the code directly after that label write:

goto a_label

Be aware that labels will "fall through". In the example below, if at line 3, math.random() !< 0.3, then the ::b:: label will run.

from the lua manual :

::a::
  print 'A'
  if math.random() < 0.3 then goto c end
::b::
  print 'B'
  if math.random() < 0.5 then goto a end
::c::
  print 'C'
  if math.random() < 0.1 then goto a else goto b end
Community content is available under CC-BY-SA unless otherwise noted.