└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Lua - Beginners Guide 2 | 3 | ## Printing 4 | ```lua 5 | print("Hello World") 6 | ``` 7 | 8 | ## Comments 9 | ```lua 10 | --this is a comment 11 | print("hello") --this is another comment 12 | -- the next line will not do anything because it is commented out 13 | --print("world") 14 | ``` 15 | 16 | ## Variables 17 | 18 | 19 | ```lua 20 | -- Different types 21 | local x = 10 --number 22 | local name = "john doe" --string 23 | local isAlive = false -- boolean 24 | local a = nil --no value or invalid value 25 | ``` 26 | 27 | **Numbers** 28 | 29 | operators 30 | - \+ addition 31 | - \- minus 32 | - \* multiply 33 | - / divide 34 | - ^ power 35 | - % modulus 36 | 37 | ```lua 38 | -- examples 39 | local a = 1 40 | local b = 2 41 | local c = a + b 42 | print(c) -- 3 43 | 44 | local d = b - a 45 | print(d) -- 1 46 | 47 | local x = 1 * 3 * 4 -- 12 48 | print(x) 49 | 50 | local y = (1+3) * 2 -- 8 51 | print(y) 52 | 53 | 54 | print(10/2) -- 5 55 | print (2^2) -- 4 56 | print(5%2) -- 1 57 | 58 | print(-b) -- -2 59 | ``` 60 | 61 | ```lua 62 | -- increment 63 | local level = 1 64 | level = level + 1 65 | print(level) -- 2 66 | ``` 67 | 68 | **Strings** 69 | ```lua 70 | -- concatenate strings 71 | local phrase = "My name is " 72 | local name = "John Doe" 73 | print(phase .. name) --My name is John Doe 74 | 75 | -- strings and numbers 76 | local age = 12 77 | local name = "Billy" 78 | print(name .. " is " .. age .. " years old") 79 | ```` 80 | 81 | **Boolean** 82 | ```lua 83 | local isAlive = true 84 | print(isAlive) --true 85 | isAlive = false 86 | print(isAlive) --false 87 | ``` 88 | 89 | ## Conditional Statements 90 | ```lua 91 | --number comparisions 92 | local age = 10 93 | if age < 18 then 94 | print("over 18") --this will not be executed 95 | end 96 | 97 | --elseif and else 98 | age = 20 99 | if age > 18 then 100 | print("dog") 101 | elseif age == 18 then 102 | print("cat") 103 | else 104 | print("mouse") 105 | end 106 | ``` 107 | 108 | **Comparison Operators** 109 | - == equality 110 | - < less than 111 | - \> greater than 112 | - <= less than or equal to 113 | - \>= greater than or equal to 114 | - ~= inequality 115 | 116 | ```lua 117 | --boolean comparision 118 | local isAlive = true 119 | if isAlive then 120 | print("dog") 121 | end 122 | 123 | --string comparisions 124 | local name = "billy" 125 | if name == "Billy" then --false 126 | print("Billy") 127 | elseif name == "billy" then --true 128 | print("billy") 129 | end 130 | 131 | ``` 132 | 133 | **Combining Statements** 134 | ```lua 135 | local x = 10 136 | if x == 10 and x < 0 then --both are true 137 | print("dog") 138 | elseif x == 100 or x < 0 then --1 or more are true 139 | print("cat") 140 | end 141 | --result: cat 142 | ``` 143 | 144 | **Nested statements** 145 | 146 | ```lua 147 | local x = 10 148 | local isAlive = true 149 | if x==10 then 150 | if isAlive == true then 151 | print("dog") 152 | else 153 | print("cat") 154 | end 155 | end 156 | ``` 157 | **Invert Value** 158 | 159 | you can also invert a value with the **not** keyword 160 | ```lua 161 | local x = 10 162 | if not x == 10 then 163 | print("here") 164 | end 165 | ``` 166 | 167 | ## Functions 168 | ```lua 169 | function printTax(price) 170 | local tax = price * 0.21 171 | print("tax:" .. tax) 172 | end 173 | 174 | printTax(200) 175 | ``` 176 | 177 | ```lua 178 | --function that returns a value 179 | function calculateTax(price) 180 | return price * 0.21 181 | end 182 | 183 | local result = calculateTax(100) 184 | print(result) 185 | 186 | --reusing the function but this time using variables 187 | local bread = 130 188 | local milk = 110 189 | 190 | local breadTax = calculateTax(bread) --27.3 191 | local milkTax = calculateTax(milk) --23.1 192 | 193 | print("Bread Tax = " .. breadTax) 194 | print("Milk Tax = " .. milkTax) 195 | ``` 196 | 197 | ```lua 198 | --multiple parameters 199 | function displayInfo(name, age, country) 200 | print(name .. " is " .. age .. " years old and is from " .. country) 201 | end 202 | 203 | displayInfo("Billy", 12, "Jupiter") 204 | ``` 205 | 206 | ## Scope 207 | Variables have different scopes. Once the end of the scope is reached the values in that scope are no longer accessable 208 | ```lua 209 | function foo() 210 | local a = 10 211 | end 212 | 213 | print(a) --nil 214 | ``` 215 | 216 | ```lua 217 | local isAlive = true 218 | if isAlive then 219 | local a = 10 220 | end 221 | 222 | print(a) --nil 223 | ``` 224 | 225 | **Global Variable** 226 | ```lua 227 | local _G.myValue = 69 228 | --doing this can sometimes be bad practice 229 | ``` 230 | 231 | ## Loops 232 | There is a few different ways you can do a loop in lua 233 | ```lua 234 | --while loop 235 | local i = 0 236 | local count = 0 237 | 238 | while i <= 10 do 239 | count = count + 1 240 | end 241 | 242 | print("count is " .. count) --count is 7 243 | 244 | 245 | --for loop 246 | count = 0 247 | for i=1, 5 do 248 | count = count + 1 249 | end 250 | print("count is " .. count) 251 | 252 | ``` 253 | 254 | **Infinite Loops** 255 | ```lua 256 | --infinite loop will never end 257 | local i = 0 258 | while i >= 0 do 259 | i = i + 1 260 | print(i) 261 | end 262 | ``` 263 | 264 | **Nested Loops** 265 | ```lua 266 | local count = 0 267 | for a=1, 10 do 268 | for b=1, 10 do 269 | count = count + 1 270 | end 271 | end 272 | print(count) -- 100 273 | ``` 274 | 275 | 276 | ## Tables 277 | ```lua 278 | --basic table 279 | local colors = { "red", "green", "blue" } 280 | 281 | print(colors[1]) --red 282 | print(colors[2]) --green 283 | print(colors[3]) --blue 284 | 285 | --using a loop to iterate though your table 286 | for i=1, #colors do 287 | print(colors[i]) 288 | end 289 | ``` 290 | 291 | **Table Manipulation** 292 | ```lua 293 | --insert 294 | local colors = { "red", "green", "blue" } 295 | table.insert(colors, "orange") 296 | local index = #colors --4 (this is the last index in the table) 297 | print(colors[index]) --orange 298 | ``` 299 | 300 | ```lua 301 | --insert at index 302 | local colors = { "red", "green", "blue" } 303 | table.insert(colors, 2, "pink") 304 | for i=1, #colors do 305 | print(colors[i]) 306 | end 307 | --red, pink, green, blue 308 | ``` 309 | 310 | ```lua 311 | --remove 312 | local colors = { "red", "green", "blue" } 313 | table.remove(colors, 1) 314 | for i=1, #colors do 315 | print(colors[i]) 316 | end 317 | -- "green", "blue" 318 | ``` 319 | 320 | 321 | **2 Dimensional Table** 322 | ```lua 323 | --tables within tables 324 | local data = { 325 | { "billy", 12 }, 326 | { "john", 20 }, 327 | { "andy", 65 } 328 | } 329 | 330 | for a=1, #data do 331 | print(data[a][1] .. " is " .. data[a][2] .. " years old") 332 | end 333 | ``` 334 | 335 | 336 | **Key Tables** 337 | 338 | 2 dimensional tables are not suited to data with different types, instead uses keys for tables 339 | ```lua 340 | local teams = { 341 | ["teamA"] = 12, 342 | ["teamB"] = 15 343 | } 344 | 345 | print(teams["teamA"]) -- 12 346 | 347 | for key,value in pairs(teams) do 348 | print(key .. ":" .. value) 349 | end 350 | ``` 351 | 352 | ```lua 353 | --insert into key table 354 | teams["teamC"] = 1 355 | ``` 356 | 357 | ```lua 358 | --remove key from table 359 | teams["teamA"] = nil 360 | ``` 361 | 362 | 363 | **Returning a Table from a Function** 364 | 365 | This can be used to return multiple values from a functions 366 | ```lua 367 | function getTeamScores() 368 | local scores = { 369 | ["teamA"] = 12, 370 | ["teamB"] = 15 371 | } 372 | return scores 373 | end 374 | 375 | local scores = getTeamScores() 376 | local total = 0 377 | for key, val in pairs(scores) do 378 | total += val 379 | end 380 | print("Total score of all teams:" .. total) 381 | ``` 382 | 383 | 384 | ## Math 385 | The math class has a number of functions for dealing with numbers. You may not need them but here is some of the more useful one functions: 386 | 387 | More: [Wiki](http://lua-users.org/wiki/MathLibraryTutorial) 388 | 389 | * abs (absolute value) 390 | ```lua 391 | local x = -10 392 | print(math.abs(x)) --result: 10 393 | local a = 10 394 | print(math.abs(a)) --result: 10 395 | ``` 396 | * ceil (round up decimal value) 397 | ```lua 398 | local x = 1.2 399 | print(math.ceil(x)) --result: 2 400 | ``` 401 | * deg (Convert value from radians to degrees) 402 | ```lua 403 | print(math.deg(math.pi)) -- result: 180 404 | ``` 405 | * floor (round down decimal value) 406 | ```lua 407 | local x = 1.2 408 | print(math.floor(x)) --result: 1 409 | ``` 410 | * pi (constant value of pi) 411 | ```lua 412 | print(math.pi) --3.1415926535898 413 | 3.1415926535898 414 | ``` 415 | * rad (Convert value from degrees to radians) 416 | ```lua 417 | print(math.rad(180)) --result: 3.1415926535898 418 | ``` 419 | * random (random number generation) 420 | ```lua 421 | --random value between 0 tand 1 422 | print(math.random()) --result: 0.0012512588885159 423 | 424 | --random integer value from 1 to 100 (both inclusive) 425 | print(math.random(100)) --result: 20 426 | 427 | --random integer value from 20 to 100 (both inclusive) 428 | print(math.random(20, 100)) --result: 54 429 | ``` 430 | * sqrt (Square root of a number) 431 | ```lua 432 | print(math.sqrt(100)) --result: 10 433 | ``` 434 | 435 | ## Modules 436 | 437 | Include code other files 438 | ```lua 439 | require("otherfile") 440 | ``` 441 | --------------------------------------------------------------------------------