[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 30, 2023, 12:58 PM +0100) b4f39e67 5de1b686

+74 -74
+74 -74
README.md
··· 44 44 type{} <-> type({}) 45 45 ``` 46 46 47 - ## Tables / arrays 47 + ## constants 48 48 49 49 ```lua 50 - t = {} 51 - t = { a = 1, b = 2 } 52 - t.a = function() ... end 50 + nil 51 + false 52 + true 53 + ``` 53 54 54 - t = { ["hello"] = 200 } 55 - t.hello 55 + ## operators and thei metatable names 56 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) 57 + ```lua 58 + -- Relational (binary) 59 + -- __eq __lt __gt __le __ge 60 + == < > <= >= 61 + ~= -- Not equal, just like != 61 62 63 + -- Arithmetic (binary) 64 + -- __add __sub __muv __div __mod __pow 65 + + - * / % ^ 62 66 63 - -- API functions 64 - table.foreach(t, function(row) --[[do something for one 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") 67 + -- Arithmetic (unary) 68 + -- __unm (unary minus) 69 + - 70 + 71 + -- Logic (and/or) 72 + nil and false --> nil 73 + false and nil --> false 74 + 0 and 20 --> 20 75 + 10 and 20 --> 20 76 + 77 + 78 + -- Length 79 + -- __len(array) 80 + #array 81 + 82 + 83 + -- Indexing 84 + -- __index(table, key) 85 + t[key] 86 + t.key 87 + 88 + -- __newindex(table, key, value) 89 + t[key]=value 90 + 91 + -- String concat 92 + -- __concat(left, right) 93 + "hello, "..name 94 + 95 + -- Call 96 + -- __call(func, ...) 73 97 ``` 74 98 75 99 ## Loops ··· 145 169 doAction('write', "Shirley", "Abed") 146 170 ``` 147 171 172 + ## Tables / arrays 173 + 174 + ```lua 175 + t = {} 176 + t = { a = 1, b = 2 } 177 + t.a = function() ... end 178 + 179 + t = { ["hello"] = 200 } 180 + t.hello 181 + 182 + -- Remember, arrays are also tables 183 + array = { "a", "b", "c", "d" } 184 + system.print(array[2]) -- "b" (one-indexed) 185 + system.print(#array) -- 4 (length) 186 + 187 + 188 + -- API functions 189 + table.foreach(t, function(row) --[[do something for one row]] end) -- Iterates over the table 190 + table.setn(t, 10) -- Sets the length of the table 191 + table.insert(t, 21) -- append (--> t[#t+1] = 21) 192 + table.insert(t, 4, 99) -- insert (--> t[4] = 99) 193 + table.getn(t) -- length (--> #t) 194 + table.concat(t, ", ") -- join (--> "a, b, c, d") 195 + table.sort(t) -- sort (--> { "a", "b", "c", "d" } 196 + table.sort(t, function(a,b) return a > b end) -- sort (--> { "d", "c", "b", "a" } 197 + table.remove(t, 4) -- remove (--> "d") 198 + ``` 199 + 148 200 ## Lookups 149 201 150 202 ```lua ··· 217 269 218 270 ``` 219 271 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 272 + ## Global Functions 273 273 274 274 ```lua 275 275 assert(x) -- Throws an error if x is false ··· 291 291 tonumber("8f", 16) -- 143 292 292 ``` 293 293 294 - ## API: Strings 294 + ## Strings 295 295 296 296 ```lua 297 297 'string'..'concatenation' -- "stringconcatenation"