Skip to content

Instantly share code, notes, and snippets.

@debarko
Created April 27, 2018 19:45
Show Gist options
  • Save debarko/e4c532b68f38903cba3e7013b82fa66b to your computer and use it in GitHub Desktop.
Save debarko/e4c532b68f38903cba3e7013b82fa66b to your computer and use it in GitHub Desktop.
LUA Cheatsheet for starting to program in Lua for experienced developers
------------------------------------------------------------------
------------------------------------------------------------------
------------LUA SUMMARY-------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
--This is a comment. For Lua, comments are invisible.
--We can type everything we want in here, without causing errors
--To make a multi-line comment we use --[[]]
---------------------------------
---------------------------------
----------VARIABLES--------------
---------------------------------
---------------------------------
--This is a variable.
x = 100
--A variable can be named everything, except Lua's keywords.
--Here's the list of keywords:
--[[
and break do else elseif
end false for function if
in local nil not or
repeat return then true until while
]]
--Also no symbols like !#@%~^&
--You can use numbers, but not as first character
--When using multiple words, use camelcasing (style preference)
--After the first word, every new word starts with an uppercase
thisIsCalledCamelCasing = 100
--You can output the value of a variable with
print(x)
--You can get the type of a variable with
type(x)
--So to output the type of a variable you do
print(type(x))
--Type of variables:
--Number
exampleNumber = 123
--String
exampleString = "Hello"
--Boolean
exampleBoolean = true
--Function
exampleFunction = function () end
--Table
exampleTable = {}
--And nil (no value)
exampleNoValue = nil
---------------------------------------
---NUMBER---
------------
exampleNumber = 100
--Use a . for decimals not a ,
exampleNumber = 123.456
--You can skip the zero for numbers lower than 1
exampleNumber = 0.456
exampleNumber = .456
exampleNumber = 10 + 10 -- Addition
exampleNumber = 30 - 10 -- Substraction
exampleNumber = 2 * 10 -- Multiplication
exampleNumber = 10 / 5 -- Division
exampleNumber = 5 ^ 2 -- Exponentiation
exampleNumber = 38 % 10 -- Modulation
---------------------------------------
---STRINGS---
-------------
--How to make one
exampleString = "Hello"
--Single quotes is fine as well
exampleString = 'Hello'
-- Use [[]] for multiple line strings
exampleString = [[ Hello
how
are
you?]]
--Combine strings with ..
exampleString = "Hello " .. "World!"
--You can use numbers as well
exampleString = "I am " .. 4 .. "years old"
---------------------------------------
---BOOLEAN---
-------------
--There are only 2 booleans:
exampleBoolean = true
exampleBoolean = false
--A condition is also a boolean
exampleBoolean = 5 > 10 --True
--Conditions are made like this:
exampleBoolean = 5 > 10 --Higher than (false)
exampleBoolean = 7 < 7 --Lower than (false)
exampleBoolean = 7 >= 7 --Higher than or equal to (true)
exampleBoolean = 7 <= 3 --Lower than or equal to (false)
exampleBoolean = 7 == 4 --Equal to (false)
exampleBoolean = 7 ~= 4 --Not equal to (true)
-- == and ~= can also be used with any type of variable
exampleBoolean = "hello" == "world" --False
exampleBoolean = true ~= false --True
exampleBoolean = {} == 14 --False
--False (Every new table is unique)
exampleBoolean = {1,2,3} == {1,2,3}
--False (Every new function is unique)
exampleBoolean = function() end == function() end
--You can use "and" to check if multiple conditions are true
exampleBoolean = 10 > 3 and 7 < 2 --False (Only one condition is true)
--You can use "or" to check if one of the conditions is true
exampleBoolean = 10 > 3 or 7 < 2 --True (One of the conditions is true)
---------------------------------------
---FUNCTIONS---
---------------
exampleFunction = function () end
--But normally typed like this:
function exampleFunction() end
--You call a function like this:
exampleFunction()
--A function can return a value
test = exampleFunction() -- test = returned value
--Without the (), it's the function itself
test = exampleFunction --test = the function exampleFunction
--You can add parameters
function exampleFunction(hello, what, test)
--hello, what and test here are what we call "parameters"
end
--We call the function with values
--The given values are what we call "arguments"
exampleFunction(10, "example", true)
--hello becomes 10
--what becomes "example"
--test becomes true
--But only in that function call
--In this function call, hello, what and test are nil again (no value)
exampleFunction()
--We can return a value with the "return" keyword
function exampleFunction(a, b)
return a + b
end
--You can use ...
--for if you're not sure how many parameters a function needs
function exampleTest(a, b, ...)
print(a, b, ...)
end
exampleTest(1,2,3,4,5,6,70,80,900,1000)
---------------------------------------
---TABLES----
-------------
exampleTable = {}
--Adding values to a table
--When creating the table:
exampleTable = {123, "hello", true}
--With table.insert()
table.insert(exampleTable, 100)
--With []
exampleTable[5] = "world"
exampleTable["test"] = "what"
--But in the case of test you should use . (style preference)
--These are called keys
exampleTable.test = "what"
--You can add keys when creating the table like this:
exampleTable = {test = "what", thing = "hello"}
--You can add any type of variable
exampleTable = {function () return "really?" end}
--You can access the variable with []
print(exampleTable[1]()) --calls the function inside the table
--You can also add tables
exampleTable = {{"wat", "oke"}, {"ehm", "sorry"}}
print(exampleTable[2][1]) --"ehm" (the first value of the second table)
--Use # to get the length of a table
print(#exampleTable)
---------------------------------------
---------------------------------------
------------FLOW CONTROL---------------
---------------------------------------
---------------------------------------
---IF STATEMENTS----
--------------------
if exampleBoolean then
--Everything inside here will only be executed when exampleBoolean (the condition)
--is not false or nil
end
--Will the inside of the if-statement be executed?
if false then end --No
if nil then end --No
if true then end --Yes
if 5 > 10 then end --Yes
if 5 < 10 then end --No
if 123 then end --Yes
if "test" then end --Yes
if {1, 2, "hello"} then end --Yes
if function () end then end --Yes
--"else" will be executed when the condition fails
if 4 > 7 then
--Will not be executed
else
--Will be executed
end
--You can add multiple elseifs
if 5 > 10 then
--Will not be executed
elseif 3 > 9 then
--Will not be executed
elseif 6 > 2 then
--Will be executed
elseif 10 > 4 then
--Will not be executed, because above already got executed.
--It's an else, meaning: if above condition fails then..
else
--Will not be executed
end
---------------------------------------
---LOOPS----
------------
for i=1,10 do
print(i) --Prints the number 1 to 10
end
--i is a variable so can be named anything
--The first number is the start, the second number is the end
--The third number is the increase every loop
for number=5,120,10 do
print(number) --Prints 5, 15, 25, etc.
if number == 45 then
break --Use break to stop a for-loop
end
end
for i=1,#exampleTable do
print(exampleTable[i]) --Print all the values in a table
end
--But for that you should use this
for i,v in ipairs(exampleTable) do
print(v) --v == exampleTable[i]
end
--Unless you want to loop through the keys
exampleTable = {test = "what", thing = 123}
--Then you use this
for k,v in pairs(exampleTable) do
print(k) --k is the key, so "test", "thing"
print(v) --v is the value, so "what" and 123
--exampleTable[k] == v
end
--This is a while loop
while exampleBoolean do
if exampleNumber > 100 then
exampleBoolean = false --exampleBoolean is now false and the loop stops
else
exampleNumber = exampleNumber + 1
end
end
--It continues till the statement is false
--Be careful with these
--If your statement will always be true your program gets stuck.
--------------------------------------
--------------------------------------
-----LOCAL VARIABLES------------------
--------------------------------------
--Only available in this file
local exampleLocal = 100
function exampleFunction()
local exampleLocal2 --Only available inside this function
if true then
--Only available in this if-statement
local exampleLocal3 = 50
for i=1,10 do
--Only available inside this for-loop
local exampleLocal4 = 20
end
end
end
--------------------------------------
--------------------------------------
-----TERNARY OPERATORS----------------
--------------------------------------
z = exampleBoolean and exampleNumber or exampleString
--Z = A and B or C
--if A is not false (or nil), then Z will become B, else Z becomes C
--This is called a ternary operator.
function exampleFunction(a, b)
a = a or 0 --if a is false or nil, then it becomes 0
b = b or 0 --if b is false or nil, then it becomes 0
return a + b
end
--------------------------------------
--------------------------------------
-----OTHER----------------------------
--------------------------------------
--Spaces and enters (new lines) don't matter
test
=
3
function exampleFunction (x, y) return x + y end if exampleFunction(10, 20) > 25 then print("test") end
------------------------------------------------------------------
--THE END---------------------------------------------------------
------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment