Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------------------------------------
- ------------------------------------------------------------------
- ------------LUA SUMMARY by Sheepolution---------------------------
- ------------------------------------------------------------------
- ------------------------------------------------------------------
- --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--------------
- ---------------------------------
- ---------------------------------
- --A variable is a word that holds a value
- example = 100
- --Now the word example holds the value 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
- --Standard math rules apply
- exampleNumber = 10 + 5 * 2 -- [5 * 2 = 10 ][ 10 + 10 = 20]
- --Use ( ) to change order
- exampleNumber = (10 + 5) * 2 -- [(10 + 5) = 15 ][ 15 * 2 = 30]
- ---------------------------------------
- ---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
- --Comparing values gets you a boolean
- exampleBoolean = 5 > 10 --True
- --Comparing works 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)
- --Use 'not' to check if something is false or nil
- exampleBoolean = not (5 > 10) --not (false), so true.
- -- == and ~= can also be used with any type of variable
- exampleBoolean = "hello" == "world" --False
- exampleBoolean = {} ~= 14 --True
- --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 comparison is true)
- --You can use "or" to check if one of the comparisons is true
- exampleBoolean = 10 > 3 or 7 < 2 --True (One of the comparisons is true)
- --You can combine them. Use ( ) to clarify groups
- exampleBoolean = (10 > 3 and 7 < 2) or (6 > 2 and 3 < 8) -- 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 ...
- --When you're not sure how many parameters a function needs
- function exampleTest(a, b, ...)
- print(a, b, ...)
- end
- exampleTest(1,2,3,4,500,60,4,80,900,1001)
- ---------------------------------------
- ---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 can use the shortcut .
- --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 = {{"what", "okay"}, {"nice", "sorry"}}
- print(exampleTable[2][1]) --"nice" (the first value of the second table)
- --Use # to get the length of a table
- print(#exampleTable) --2
- ---------------------------------------
- ---------------------------------------
- ------------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 not false 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
- --You can make a local variable, but assign a value later on
- local exampleLocal
- if true then
- exampleLocal = 100
- end
- print(exampleLocal) --100
- --You can create multiple local variables at the same time
- local example, test, thing = 10, 50, 20
- --------------------------------------
- --------------------------------------
- -----TERNARY OPERATORS----------------
- --------------------------------------
- --------------------------------------
- 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.
- A, B, C = false, "hello", "world"
- Z = A and B or C --Z = "world", because A is false.
- 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---------------------------------------------------------
- ------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement