[READ-ONLY] Mirror of https://github.com/thoda-dev/du-lua-cheatsheet. a simple cheatsheet for lua in Dual Universe
dual-universe lua
0

Configure Feed

Select the types of activity you want to include in your feed.

Update README.md

Thomas (Jan 23, 2023, 7:38 PM +0100) a9729ab9 6d59ab7a

+353 -2
+353 -2
README.md
··· 1 - # lua-cheatsheet 2 - a simple cheatsheet for lua 1 + # du-lua-cheatsheet 2 + a simple cheatsheet for lua in Dual Universe 3 + 4 + ## Menu 5 + 6 + * [Comments](#comments) 7 + * [invoking functions](#invoking-functions) 8 + * [Tables / arrays](#tables--arrays) 9 + * [Loops](#loops) 10 + * [Conditions](#conditions) 11 + * [variables](#variables) 12 + * [Functions](#functions) 13 + * [Lookups](#lookups) 14 + * [Metatables](#metatables) 15 + * [classes](#classes) 16 + * [constants](#constants) 17 + * [operators and thei metatable names](#operators-and-thei-metatable-names) 18 + * [API Global Functions](#api-global-functions) 19 + * [API Strings](#api-strings) 20 + * [Coroutine](#coroutine) 21 + 22 + # Lua Cheatsheet 23 + 24 + ## Comments 25 + 26 + ```lua 27 + -- comment 28 + 29 + --[[ 30 + multiline 31 + comment 32 + ]] 33 + ``` 34 + 35 + ## invoking functions 36 + 37 + ```lua 38 + system.print() 39 + system.print("Hi") 40 + 41 + -- You can omit parentheses if the argument is one string or table literal 42 + system.print "Hello World" <-> system.print("Hello World") 43 + f{x=10, y=20} <-> f({x=10, y=20}) 44 + type{} <-> type({}) 45 + ``` 46 + 47 + ## Tables / arrays 48 + 49 + ```lua 50 + t = {} 51 + t = { a = 1, b = 2 } 52 + t.a = function() ... end 53 + 54 + t = { ["hello"] = 200 } 55 + t.hello 56 + 57 + -- Remember, arrays are also tables 58 + array = { "a", "b", "c", "d" } 59 + system.print(array[2]) -- "b" (one-indexed) 60 + system.print(#array) -- 4 (length) 61 + 62 + 63 + -- API functions 64 + table.foreach(t, function(row) ... end) -- Iterates over the table 65 + table.setn(t, 10) -- Sets the length of the table 66 + table.insert(t, 21) -- append (--> t[#t+1] = 21) 67 + table.insert(t, 4, 99) -- insert (--> t[4] = 99) 68 + table.getn(t) -- length (--> #t) 69 + table.concat(t, ", ") -- join (--> "a, b, c, d") 70 + table.sort(t) -- sort (--> { "a", "b", "c", "d" } 71 + table.sort(t, function(a,b) return a > b end) -- sort (--> { "d", "c", "b", "a" } 72 + table.remove(t, 4) -- remove (--> "d") 73 + ``` 74 + 75 + ## Loops 76 + 77 + ```lua 78 + while condition do 79 + end 80 + 81 + for i = 1,5 do 82 + end 83 + 84 + for i = start,finish,delta do 85 + end 86 + 87 + for k,v in pairs(tab) do 88 + end 89 + 90 + repeat 91 + until condition 92 + 93 + -- Breaking out: 94 + while x do 95 + if condition then break end 96 + end 97 + ``` 98 + 99 + ## Conditions 100 + 101 + ```lua 102 + if condition then 103 + system.print("yes") 104 + elseif condition then 105 + system.print("maybe") 106 + else 107 + system.print("no") 108 + end 109 + ``` 110 + 111 + # variables 112 + 113 + ```lua 114 + local x = 2 115 + two = 2 116 + two, four = 2, 4 117 + ``` 118 + 119 + ## Functions 120 + 121 + ```lua 122 + function myFunction() 123 + return 1 124 + end 125 + 126 + function myFunctionWithArgs(a, b) 127 + -- ... 128 + end 129 + 130 + myFunction() 131 + 132 + anonymousFunctions(function() 133 + -- ... 134 + end) 135 + 136 + -- Not exported in the module 137 + local function myPrivateFunction() 138 + end 139 + 140 + -- Splats 141 + function doAction(action, ...) 142 + system.print("Doing '"..action.."' to", ...) -- "Doing 'write' to", "Shirley", "Abed" 143 + end 144 + 145 + doAction('write', "Shirley", "Abed") 146 + ``` 147 + 148 + ## Lookups 149 + 150 + ```lua 151 + mytable = { x = 2, y = function() .. end } 152 + 153 + -- The same: 154 + mytable.x 155 + mytable['x'] 156 + 157 + -- Syntactic sugar, these are equivalent: 158 + mytable.y(mytable) 159 + mytable:y() 160 + 161 + mytable.y(mytable, a, b) 162 + mytable:y(a, b) 163 + 164 + function X:y(z) .. end 165 + function X.y(self, z) .. end 166 + ``` 167 + 168 + -- Metatables 169 + 170 + ```lua 171 + mt = {} 172 + 173 + -- A metatable is simply a table with functions in it. 174 + mt.__tostring = function() return "lol" end 175 + mt.__add = function(b) ... end -- a + b 176 + mt.__mul = function(b) ... end -- a * b 177 + mt.__index = function(k) ... end -- Lookups (a[k] or a.k) 178 + mt.__newindex = function(k, v) ... end -- Setters (a[k] = v) 179 + 180 + -- Metatables allow you to override behavior of another table. 181 + mytable = {} 182 + setmetatable(mytable, mt) 183 + 184 + system.print(myobject) 185 + ``` 186 + 187 + ## classes 188 + 189 + ```lua 190 + -- A class is simply a table with a metatable. 191 + -- The metatable is used to create new instances of the class. 192 + -- The metatable's __index is used to lookup methods on the class. 193 + 194 + -- Create a new class 195 + Account = {} 196 + 197 + function Account:new(balance) 198 + local t = setmetatable({}, { __index = Account }) 199 + 200 + -- Your constructor stuff 201 + t.balance = (balance or 0) 202 + return t 203 + end 204 + 205 + function Account:withdraw(amount) 206 + print("Withdrawing "..amount.."...") 207 + self.balance = self.balance - amount 208 + self:report() 209 + end 210 + 211 + function Account:report() 212 + system.print("Your current balance is: "..self.balance) 213 + end 214 + 215 + a = Account:new(9000) 216 + a:withdraw(200) -- method call 217 + 218 + ``` 219 + 220 + ## constants 221 + 222 + ```lua 223 + nil 224 + false 225 + true 226 + ``` 227 + 228 + ## operators and thei metatable names 229 + 230 + ```lua 231 + -- Relational (binary) 232 + -- __eq __lt __gt __le __ge 233 + == < > <= >= 234 + ~= -- Not equal, just like != 235 + 236 + -- Arithmetic (binary) 237 + -- __add __sub __muv __div __mod __pow 238 + + - * / % ^ 239 + 240 + -- Arithmetic (unary) 241 + -- __unm (unary minus) 242 + - 243 + 244 + -- Logic (and/or) 245 + nil and false --> nil 246 + false and nil --> false 247 + 0 and 20 --> 20 248 + 10 and 20 --> 20 249 + 250 + 251 + -- Length 252 + -- __len(array) 253 + #array 254 + 255 + 256 + -- Indexing 257 + -- __index(table, key) 258 + t[key] 259 + t.key 260 + 261 + -- __newindex(table, key, value) 262 + t[key]=value 263 + 264 + -- String concat 265 + -- __concat(left, right) 266 + "hello, "..name 267 + 268 + -- Call 269 + -- __call(func, ...) 270 + ``` 271 + 272 + ## API Global Functions 273 + 274 + ```lua 275 + assert(x) -- Throws an error if x is false 276 + assert(x, "failed") -- Throws an error if x is false, with the message "failed" 277 + 278 + type(x) -- Returns the type of x ("nil" | "number" | "string" | "boolean" | "table" | "function" | "thread") 279 + 280 + -- Does /not/ invoke meta methods (__index and __newindex) 281 + rawset(t, index, value) -- Like t[index] = value 282 + rawget(t, index) -- Like t[index] 283 + 284 + _G -- Global context 285 + setfenv(1, {}) -- 1: current function, 2: caller, and so on -- {}: the new _G 286 + 287 + pairs(t) -- iterable list of {key, value} 288 + ipairs(t) -- iterable list of {index, value} 289 + 290 + tonumber("34") -- 34 291 + tonumber("8f", 16) -- 143 292 + ``` 293 + 294 + ## API: Strings 295 + 296 + ```lua 297 + 'string'..'concatenation' -- "stringconcatenation" 298 + 299 + s = "Hello" 300 + s:upper() -- Just like string.upper(s): "HELLO" 301 + s:lower() -- Just like string.lower(s): "hello" 302 + s:len() -- Just like #s or string.len(s): 5 303 + 304 + s:find("e") -- Just like string.find(s, "e"): 2 index (in lua index start ar 1, not 0) or nil if not found 305 + s:gfind("l") -- Just like string.gfind(s, "l"): iterator of all "l" in s or nil if not found 306 + 307 + s:match("e") -- Just like string.match(s, "e"): "e" or nil if not found 308 + s:gmatch("l") -- Just like string.gmatch(s, "l"): iterator of all "l" in s or nil if not found 309 + 310 + s:sub(2, 4) -- Just like string.sub(s, 2, 4): "ell" 311 + s:gsub("l", "i", 10) -- Just like string.gsub(s, "l", "i", 10): "Heiio" (10 is the number of replacement) 312 + 313 + s:rep(10) -- Just like string.rep(s, 10): "HelloHelloHelloHelloHelloHelloHelloHelloHelloHello" 314 + s:char(65) -- Just like string.char(65): "A" 315 + s:reverse() -- Just like string.reverse(s): "olleH" 316 + s:byte() -- Just like string.byte(s): 72 (the first byte of the string) 317 + s:format("%s world") -- Just like string.format("%s world", s): "Hello world" 318 + ``` 319 + 320 + ## Coroutine 321 + 322 + ```lua 323 + -- A coroutine is a function that can be paused and resumed. 324 + -- It is useful for implementing cooperative multitasking. 325 + 326 + -- Create a new coroutine 327 + co = coroutine.create(function() --[[do something]] end) 328 + 329 + -- Resume a coroutine 330 + coroutine.resume(co) -- true | false, error 331 + 332 + -- Yield a coroutine 333 + coroutine.yield() 334 + 335 + -- Get the status of a coroutine 336 + coroutine.status(co) -- "running" | "suspended" | "dead" 337 + 338 + -- example 339 + co = coroutine.create(function() 340 + for i = 1, 10 do 341 + system.print(i) -- print 1 to 10 342 + coroutine.yield() -- pause 343 + end 344 + end) 345 + 346 + coroutine.resume(co) -- 1 347 + coroutine.resume(co) -- 2 348 + coroutine.resume(co) -- 3 349 + coroutine.resume(co) -- 4 350 + coroutine.resume(co) -- 5 351 + -- ... 352 + 353 + ```