daily pastebin goal
63%
SHARE
TWEET

Lua Summary

a guest Jun 11th, 2016 184 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ------------------------------------------------------------------
  2. ------------------------------------------------------------------
  3. ------------LUA SUMMARY by Sheepolution---------------------------
  4. ------------------------------------------------------------------
  5. ------------------------------------------------------------------
  6. --This is a comment. For Lua, comments are invisible.
  7. --We can type everything we want in here, without causing errors
  8. --To make a multi-line comment we use --[[]]
  9.  
  10.  
  11. ---------------------------------
  12. ---------------------------------
  13. ----------VARIABLES--------------
  14. ---------------------------------
  15. ---------------------------------
  16. --A variable is a word that holds a value
  17. example = 100
  18.  
  19. --Now the word example holds the value 100
  20.  
  21.  
  22. --A variable can be named everything, except Lua's keywords.
  23. --Here's the list of keywords:
  24. --[[
  25.     and       break     do        else      elseif
  26.     end       false     for       function  if
  27.     in        local     nil       not       or
  28.     repeat    return    then      true      until     while
  29. ]]
  30. --Also no symbols like !#@%~^&
  31. --You can use numbers, but not as first character
  32.  
  33. --When using multiple words, use camelcasing (style preference)
  34. --After the first word, every new word starts with an uppercase
  35. thisIsCalledCamelCasing = 100
  36.  
  37. --You can output the value of a variable with
  38. print(x)
  39.  
  40. --You can get the type of a variable with
  41. type(x)
  42.  
  43. --So to output the type of a variable you do
  44. print(type(x))
  45.  
  46.  
  47. --Type of variables:
  48.  
  49. --Number
  50. exampleNumber = 123
  51.  
  52. --String
  53. exampleString = "Hello"
  54.  
  55. --Boolean
  56. exampleBoolean = true
  57.  
  58. --Function
  59. exampleFunction = function () end
  60.  
  61. --Table
  62. exampleTable = {}
  63.  
  64. --And nil (no value)
  65. exampleNoValue = nil
  66.  
  67. ---------------------------------------
  68. ---NUMBER---
  69. ------------
  70. exampleNumber = 100
  71.  
  72. --Use a . for decimals not a ,
  73. exampleNumber = 123.456
  74.  
  75. --You can skip the zero for numbers lower than 1
  76. exampleNumber = 0.456
  77. exampleNumber = .456
  78.  
  79. exampleNumber = 10 + 10 -- Addition
  80. exampleNumber = 30 - 10 -- Substraction
  81. exampleNumber = 2 * 10  -- Multiplication
  82. exampleNumber = 10 / 5  -- Division
  83. exampleNumber = 5 ^ 2   -- Exponentiation
  84. exampleNumber = 38 % 10 -- Modulation
  85.  
  86. --Standard math rules apply
  87. exampleNumber = 10 + 5 * 2 -- [5 * 2 = 10 ][ 10 + 10 = 20]
  88.  
  89. --Use ( ) to change order
  90. exampleNumber = (10 + 5) * 2 -- [(10 + 5) = 15 ][ 15 * 2 = 30]
  91.  
  92. ---------------------------------------
  93. ---STRINGS---
  94. -------------
  95. --How to make one
  96. exampleString = "Hello"
  97.  
  98. --Single quotes is fine as well
  99. exampleString = 'Hello'
  100.  
  101. -- Use [[]] for multiple line strings
  102. exampleString = [[ Hello
  103. how
  104. are
  105. you?]]
  106.  
  107. --Combine strings with ..
  108. exampleString = "Hello " .. "World!"
  109.  
  110. --You can use numbers as well
  111. exampleString = "I am " .. 4 .. "years old"
  112.  
  113. ---------------------------------------
  114. ---BOOLEAN---
  115. -------------
  116. --There are only 2 booleans:
  117. exampleBoolean = true
  118. exampleBoolean = false
  119.  
  120. --Comparing values gets you a boolean
  121. exampleBoolean = 5 > 10 --True
  122.  
  123. --Comparing works like this:
  124. exampleBoolean = 5 > 10 --Higher than (false)
  125. exampleBoolean = 7 < 7 --Lower than (false)
  126. exampleBoolean = 7 >= 7 --Higher than or equal to (true)
  127. exampleBoolean = 7 <= 3 --Lower than or equal to (false)
  128. exampleBoolean = 7 == 4 --Equal to (false)
  129. exampleBoolean = 7 ~= 4 --Not equal to (true)
  130.  
  131.  
  132. --Use 'not' to check if something is false or nil
  133. exampleBoolean = not (5 > 10) --not (false), so true.
  134.  
  135. -- == and ~= can also be used with any type of variable
  136. exampleBoolean = "hello" == "world" --False
  137. exampleBoolean = {} ~= 14 --True
  138.  
  139. --False (Every new table is unique)
  140. exampleBoolean = {1,2,3} == {1,2,3}
  141.  
  142. --False (Every new function is unique)
  143. exampleBoolean = function() end == function() end
  144.  
  145. --You can use "and" to check if multiple conditions are true
  146. exampleBoolean = 10 > 3 and 7 < 2 --False (Only one comparison is true)
  147.  
  148. --You can use "or" to check if one of the comparisons is true
  149. exampleBoolean = 10 > 3 or 7 < 2 --True (One of the comparisons is true)
  150.  
  151. --You can combine them. Use ( ) to clarify groups
  152. exampleBoolean = (10 > 3 and 7 < 2) or (6 > 2 and 3 < 8) -- True
  153.  
  154. ---------------------------------------
  155. ---FUNCTIONS---
  156. ---------------
  157. exampleFunction = function () end
  158.  
  159. --But normally typed like this:
  160. function exampleFunction() end
  161.  
  162. --You call a function like this:
  163. exampleFunction()
  164.  
  165. --A function can return a value
  166. test = exampleFunction() -- test = returned value
  167.  
  168. --Without the (), it's the function itself
  169. test = exampleFunction --test = the function exampleFunction
  170.  
  171. --You can add parameters
  172. function exampleFunction(hello, what, test)
  173.     --hello, what and test here are what we call "parameters"
  174. end
  175.  
  176. --We call the function with values
  177. --The given values are what we call "arguments"
  178. exampleFunction(10, "example", true)
  179. --hello becomes 10
  180. --what becomes "example"
  181. --test becomes true
  182. --But only in that function call
  183.  
  184. --In this function call, hello, what and test are nil again (no value)
  185. exampleFunction()
  186.  
  187. --We can return a value with the "return" keyword
  188.  
  189. function exampleFunction(a, b)
  190.     return a + b
  191. end
  192.  
  193. --You can use ...
  194. --When you're not sure how many parameters a function needs
  195. function exampleTest(a, b, ...)
  196.     print(a, b, ...)
  197. end
  198.  
  199. exampleTest(1,2,3,4,500,60,4,80,900,1001)
  200.  
  201. ---------------------------------------
  202. ---TABLES----
  203. -------------
  204. exampleTable = {}
  205.  
  206. --Adding values to a table
  207. --When creating the table:
  208. exampleTable = {123, "hello", true}
  209.  
  210. --With table.insert()
  211. table.insert(exampleTable, 100)
  212.  
  213. --With []
  214. exampleTable[5] = "world"
  215. exampleTable["test"] = "what"
  216.  
  217. --But in the case of test you can use the shortcut .
  218. --These are called keys
  219. exampleTable.test = "what"
  220.  
  221. --You can add keys when creating the table like this:
  222. exampleTable = {test = "what", thing = "hello"}
  223.  
  224. --You can add any type of variable
  225. exampleTable = {function () return "really?" end}
  226.  
  227. --You can access the variable with []
  228. print(exampleTable[1]()) --calls the function inside the table
  229.  
  230. --You can also add tables
  231. exampleTable = {{"what", "okay"}, {"nice", "sorry"}}
  232.  
  233. print(exampleTable[2][1]) --"nice" (the first value of the second table)
  234.  
  235. --Use # to get the length of a table
  236. print(#exampleTable) --2
  237.  
  238. ---------------------------------------
  239. ---------------------------------------
  240. ------------FLOW CONTROL---------------
  241. ---------------------------------------
  242. ---------------------------------------
  243. ---IF STATEMENTS----
  244. --------------------
  245. if exampleBoolean then
  246.     --Everything inside here will only be executed when exampleBoolean (the condition)
  247.     --is NOT false or nil
  248. end
  249.  
  250. --Will the inside of the if-statement be executed?
  251. if false then end --No
  252. if nil then end --No
  253. if true then end --Yes
  254. if not false then end --Yes
  255. if 5 > 10 then end --Yes
  256. if 5 < 10 then end --No
  257. if 123 then end --Yes
  258. if "test" then end --Yes
  259. if {1, 2, "hello"} then end --Yes
  260. if function () end then end --Yes
  261.  
  262. --"else" will be executed when the condition fails
  263. if 4 > 7 then
  264.     --Will not be executed
  265. else
  266.     --Will be executed
  267. end
  268.  
  269. --You can add multiple elseifs
  270.  
  271. if 5 > 10 then
  272.     --Will not be executed
  273. elseif 3 > 9 then
  274.     --Will not be executed
  275. elseif 6 > 2 then
  276.     --Will be executed
  277. elseif 10 > 4 then
  278.     --Will not be executed, because above already got executed.
  279.     --It's an else, meaning: if above condition fails then..
  280. else
  281.     --Will not be executed
  282. end
  283.  
  284. ---------------------------------------
  285. ---LOOPS----
  286. ------------
  287. for i=1,10 do
  288.     print(i) --Prints the number 1 to 10
  289. end
  290.  
  291. --i is a variable so can be named anything
  292. --The first number is the start, the second number is the end
  293. --The third number is the increase every loop
  294. for number=5,120,10 do
  295.     print(number) --Prints 5, 15, 25, etc.
  296.     if number == 45 then
  297.         break --Use break to stop a for-loop
  298.     end
  299. end
  300.  
  301. for i=1,#exampleTable do
  302.     print(exampleTable[i]) --Print all the values in a table
  303. end
  304.  
  305. --But for that you should use this
  306. for i,v in ipairs(exampleTable) do
  307.     print(v) --v == exampleTable[i]
  308. end
  309.  
  310. --Unless you want to loop through the keys
  311. exampleTable = {test = "what", thing = 123}
  312.  
  313. --Then you use this
  314. for k,v in pairs(exampleTable) do
  315.     print(k) --k is the key, so "test", "thing"
  316.     print(v) --v is the value, so "what" and 123
  317.              --exampleTable[k] == v
  318. end
  319.  
  320. --This is a while loop
  321. while exampleBoolean do
  322.     if exampleNumber > 100 then
  323.         exampleBoolean = false --exampleBoolean is now false and the loop stops
  324.     else
  325.         exampleNumber = exampleNumber + 1
  326.     end
  327. end
  328.  
  329. --It continues till the statement is false
  330. --Be careful with these
  331. --If your statement will always be true your program gets stuck.
  332.  
  333. --------------------------------------
  334. --------------------------------------
  335. -----LOCAL VARIABLES------------------
  336. --------------------------------------
  337. --------------------------------------
  338. --Only available in this file
  339. local exampleLocal = 100
  340.  
  341. function exampleFunction()
  342.     local exampleLocal2 --Only available inside this function
  343.     if true then
  344.         --Only available in this if-statement
  345.         local exampleLocal3 = 50
  346.         for i=1,10 do
  347.             --Only available inside this for-loop
  348.             local exampleLocal4 = 20
  349.         end
  350.     end
  351. end
  352.  
  353. --You can make a local variable, but assign a value later on
  354. local exampleLocal
  355.  
  356. if true then
  357.     exampleLocal = 100
  358. end
  359.  
  360. print(exampleLocal) --100
  361.  
  362. --You can create multiple local variables at the same time
  363. local example, test, thing = 10, 50, 20
  364.  
  365. --------------------------------------
  366. --------------------------------------
  367. -----TERNARY OPERATORS----------------
  368. --------------------------------------
  369. --------------------------------------
  370. Z = A and B or C
  371.  
  372. --if A is not false (or nil), then Z will become B, else Z becomes C
  373. --This is called a ternary operator.
  374.  
  375. A, B, C = false, "hello", "world"
  376. Z = A and B or C --Z = "world", because A is false.
  377.  
  378. function exampleFunction(a, b)
  379.     a = a or 0 --if a is false or nil, then it becomes 0
  380.     b = b or 0 --if b is false or nil, then it becomes 0
  381.     return a + b
  382. end
  383.  
  384. --------------------------------------
  385. --------------------------------------
  386. -----OTHER----------------------------
  387. --------------------------------------
  388. --------------------------------------
  389. --Spaces and enters (new lines) don't matter
  390. test
  391. =
  392. 3
  393. function exampleFunction (x, y) return x + y end if exampleFunction(10, 20) > 25 then print("test") end
  394. ------------------------------------------------------------------
  395. --THE END---------------------------------------------------------
  396. ------------------------------------------------------------------
RAW Paste Data
Top