You are on page 1of 7
aernora Lua cheatsheet Lua Comments comment. [ muttitine comment. }) Invoking fuunétions print() print(*hi") =+ You can omit parentheses if the argunent is one string or table Literal print "Helto World" print("Helto World") dofite ‘a.tua" dofite ('a-tua") print [[a multi-Line print({[a mutti-Line message] essage)]) Fixe18, y=20} type} #({x=18, y=20}) type({}) Tables / arrays asab-2} tia = function() ... end t= { [*hetior) = 200 } tunetto == Revenber, arrays are also tables array = { "a", "bY, “c*, "0" } print(array(2}) "= *b* (one-indexed) print(tarray) == 4 (length) Loops htps:idevints.iotua wr sernora Lua cheatsheet while condition do end for i= 1,5 do end for i = start,finish,delta do end for kv in pairs(tab) do end repeat untill condition - Breaking out: hile x 6 if condition then break end end Conditionals if condition then print("yes") elsif condition then print ("maybe") else print(*no") Variables Local x = 2 two, four = 2, 4 Funitions function myFunction() return 1 end function ayFunctioniithArgs(a, b) htps:idevints.iotua an aernora Lua cheatsheet end ayFunction() anonynousFunctions(function( ) end) “ == Not exported in the modute Local function myPrivaterunction() end -- Splats function coAction(action, ...) print("Doing '"..action.."' to", ...) =-> print("Doing ‘write’ to", "Shirley end "Abed" ) donction('write’, "Shirley", "Abec") Lookups aytable = { x= 2, y = function() .. end } ‘The sane: aytable.x aytablet'x'] -+ Syntactic sugar, these are equivalent: aytable.y(aytable) ytable:y() aytable.y(aytable, a, b) aytable:y(a, b) function X:y(z) .. end function X.y(self, 2) -. end Metatables a in -+ A-metatable is simply a table with functions in it. mt.__tostring = function() return "Lol" end at, function(b) ... end a+b at, = function(b) ... end ab mt._vindex = function(k) ... end Lookups (atk) or a.k) int. __newindex function(k, v) ... end Setters (alk) = v) -+ Metatables allow you to override behavior of another table, htps:idevints.iotua a7

You might also like