Jason Follas.com
Navigation
Main Page
Random Page
Create a new Page
All Pages
Categories
Administration
File Management
Login/Logout
Language Selection
Your Profile
Create Account
Quick Search
Advanced Search »
Back
History
The LUA 5.1 Programming Language
===Intro=== WoW add-ons are developed using the LUA 5.1 programming language: {BR}{BR}[http://www.lua.org/manual/5.1/|LUA 5.1 Manual] {BR} [http://lua-users.org/files/wiki_insecure/users/thomasl/luarefv51.pdf|A Condensed Summary] Of course, the LUA language itself does not implement the WoW API, which uses an event-driven model, so simply reading the manual will not help you to write that killer add-on. But, you've got to crawl before you can run, so let's dive into some of the syntax features of LUA itself. ===Reserved Keywords=== A lot can be learned about a language just by knowing what words are reserved keywords (and thus, cannot be used as variable names). {{{{and break do else elseif end false for function if in local nil not or repeat return then true until while}}}} ===Comment=== {{{{<nowiki>-- This is a single line comment --[[ This is a block of comment text ]]</nowiki>}}}} ===Arithmetic Operators=== {{{{<nowiki>+ addition 1 + 2 => 3 - substraction 2 - 1 => 1 * multiplication 3 * 4 => 12 / division 9 / 3 => 3 % modulo 8 % 6 => 2 ^ exponent 2 ^ 5 => 32</nowiki>}}}} ===Relational Operators=== {{{{== equality ~= inequality < less-than <= less-than or equal > greater-than >= greater-than or equal}}}} ===Logical Operators=== '''false''' and '''nil''' are both considered '''false''', everything else is '''true'''. Assigning the results of a logical operation to a variable might give some unexpected results (stated below). {{{{and (returns first argument if false/nil, otherwise returns second argument) or (returns the first argument if not false/nil, otherwise returns the second argument) not (returns true or false, representing the opposite of the supplied argument) 10 or 20 --> 10 10 and 20 --> 20 nil or 10 --> 10 nil and 10 --> nil}}}} ===Variable Assignment=== {{{{a = 1 a, b = 1, 2 Multiple assignment at the same time a, b = b, a Variable Swap _, _, a = 1, 2, 3 Common usage of "throw away" variables}}}} ===Literals=== {{{{aString = 'single quotes work' bString = "double quotes work" cString = <nowiki>[[hell, even double square brackets work]]</nowiki> dNumber = 12345.6789}}}} ===Concatenation=== The double-dot (..) represents the concatenation operator. {{{{name = "Jason" print("Hello, "..name..", it is very good to meet you!") results: Hello, Jason, it is very good to meet you!}}}} ===Length Operator=== The pound-sign (#) preceeding a variable returns the length of that variable. If the variable is a string, then the returned value is the number of characters. If the variable is a table, then the returned value is the highest index number such that the table element at the next higher index is nil. {{{{name = "Jason" print(#name) results: 5}}}} ===IF Statements=== {{{{if i == 1 then print("One") elseif i==2 then print("Two") else print(i) end}}}} ===FOR Statements=== {{{{<nowiki>for i = 1, 5, 1 -- start, limit, increment do print(i.."\n") end results: 1 2 3 4 5</nowiki>}}}} ===WHILE and REPEAT Statements=== {{{{<nowiki>i = 3 while i > 1 do print(i.."\n") i = i - 1 end repeat print(i.."\n") i = i + 1 until i = 3 results: 3 2 <-- last output of the WHILE 1 <-- first output of the REPEAT 2</nowiki>}}}} ===Functions=== {{{{function add (input1, input2) return input1 + input2 end (equivalent to) add = function(input1, input2) return input1 + input2 end function MultiValue () return 1, 2, 3, 4 end i = add(1, 2) print(i) result => 3 _, _, a, b = MultiValue() print(a..","..b) result => 3,4 }}}} ===Tables=== A table is very much like a Dictionary in other languages. By default, tables use a one-based index as the key: {{{{<nowiki>a = {101, 202, 303, 404, 505} print(a[2]) result => 202</nowiki>}}}} Instead of accepting the default key, though, it can be specified. Any unspecified key names will assume the next available one-based integer index: {{{{<nowiki>a = {[3] = 101, [4] = 202, 303, 404, fifth = 505} print(a[2]) result => 404 print(a[3]) result => 101 print(a.fifth) result => 505</nowiki>}}}} The square bracket notation for key assignment substitutes the value inside the brackets (either a literal or a variable's value): {{{{<nowiki>name = "Fred" a = {[name] = "Flintstone", name = "Barney"} print(a[name]) result => Flintstone print(a.name) result => Barney print(a.Fred) result => Flintstone </nowiki>}}}} Note that in this sense, table "properties" are indeed case sensitive, since they are just syntactic sugar for a string-based key: {{{{<nowiki>a = { } a.Name = "Jason" is equivalent to: a = { ["Name"] = "Jason" } but not: a = { ["name"] = "Jason" } </nowiki>}}}} ''(work in progress)''
ScrewTurn Wiki
version 2.0.13. Some of the icons created by
FamFamFam
.