|
|
Line 3: |
Line 3: |
| |<big>'''Do not edit!'''</big><br><small>The creator of this subpage does not want it to be edited without permission.</small> | | |<big>'''Do not edit!'''</big><br><small>The creator of this subpage does not want it to be edited without permission.</small> |
| |} | | |} |
|
| |
| ==Hello World==
| |
|
| |
| The language I'm teaching you is called ''Lua''. A tradition of programming languages is to start off the learner with a ''Hello World'' program. So what is a ''Hello World'' program? It is a program that writes the words "Hello World" to the [[Output|output]]. Here it is in Lua:
| |
| {{Example|{{Code and output|code=
| |
| print("Hello World")
| |
| |output=
| |
| Hello World
| |
| |fit=output}}}}
| |
|
| |
| Ta-da! For now and the rest of the tutorial, that box on the left is the output. Keep that in mind. [[Function_Dump/Core_Functions#print_.28.C2.B7.C2.B7.C2.B7.29|Print]] is our function for writing to the output. When we want to print string literals (words, like ''Hello World'') to the output, we '''must''' put them in quotes. Here's another example:
| |
|
| |
| {{Example|{{Code and output|code=
| |
| print("Hi Newcomer!")
| |
| |output=
| |
| Hi Newcomer!
| |
| |fit=output}}}}
| |
|
| |
|
| |
| __TOC__
| |
|
| |
|
| |
| ==Beginning==
| |
| ===Terminology===
| |
|
| |
| There are three terms I want you to know before we continue:
| |
| *{{`|code}} refers to '''anything''' written in Lua.
| |
| *{{`|chunk}} refers to '''one''' line of {{`|code}} in Lua.
| |
| *{{`|script}} refers to '''multiple''' {{`|chunk}}s that accomplish a specific task in Lua.
| |
| *{{`|character}} refers to any of the [http://wiki.roblox.com/index.php/File:Ascii_Table.png ASCII Values] (numbers, letters, spaces, enter key, down arrow, etc.)
| |
|
| |
| ===[[Comments_(Scripting)|Comments]]===
| |
|
| |
| Before I start explaining the stuff you can do in Lua to you, there is something I need to explain to you that I'm going to use a lot in this tutorial: ''Commenting''. Comments are things that are '''not''' executed by Lua. A comment can contain any characters because it doesn't need to be read.
| |
|
| |
| ====[[Comments_(Scripting)#Single-line_Comments|One-Line Comments]]====
| |
|
| |
| A one-line comment is made with {{`|--}}. It lasts for the rest of the line. Here are some examples:
| |
| {{Example|{{Code and output|code=
| |
| print("Hello world") -- This will print Hello world to the output!
| |
| -- I'm invisible! print("Hi"). Hi won't be printed to the output!
| |
| |output=
| |
| Hello world
| |
| |fit=output}}}}
| |
|
| |
| ====[[Comments_(Scripting)#Block_Comments|Multi-Line Comments]]====
| |
|
| |
| A multi-line comment starts with {{`|<nowiki>--[[</nowiki>}} and ends with {{`|<nowiki>]]</nowiki>}}. Everything between the opening and the closing is treated as a comment, even if the comment is only in the middle of the line.
| |
| {{Example|{{Code and output|code=
| |
| print("Hello World")--[[
| |
| I'm a very long comment.
| |
| In fact, I last multiple lines.
| |
| Should I be discovered, I must… but that won't happen.
| |
| ]]|output=
| |
| Hello World
| |
| |fit=output}}}}
| |
|
| |
| ''A multi-line comment '''cannot''' be placed inside of a string literal (inside of quotation marks)!'' You must put it elsewhere. Here are more examples:
| |
| {{Example|{{Code and output|code=
| |
| print("Hello M--[[I'll never be found!]]y Friend!")
| |
| --[[They'll never catch me!]]print("HI")
| |
| |output=
| |
| Hello M--[[I'll never be found!]]y Friend!
| |
| HI
| |
| }}}}
| |
|
| |
| There are also [[Comments_(Scripting)#Leveled_Comments|leveled comments]], but I won't go into those. They're just like multi-line comments, except they have different levels. I never use them.
| |
|
| |
| ===[[Variable|Variables]]===
| |
|
| |
| A [[Variable|variable]] is just a container. It holds a value. For a good understanding, please read the [[Variable|page on variables]] from sections [[Variable#Assignment|1]] to [[Variable#Swapping|3.1]], ignoring the section on Scope. We'll get to that later. Here are just some examples of variables:
| |
|
| |
| {{Example|<code lua>
| |
| x = 3
| |
| y = 4
| |
| aVariable = 6
| |
| iHave134141NumbersInMyNameAndABooleanValue = true
| |
| </code>}}
| |
|
| |
| Remember that saying {{`|<nowiki>variable = someValue</nowiki>}} is called ''declaring a variable''.
| |
|
| |
| ==Types and Values==
| |
| ===[[Nil]]===
| |
|
| |
| For our intents and purposes, [[Nil|''nil'']] means nothing. If you '''really''' want an in-depth guide, read [[Nil|the page on nil]]. Here are some examples:
| |
| {{Example|{{Code and output|code=
| |
| x1 = nil
| |
| x2 = nil
| |
| x3 = nil
| |
| x4 = nil
| |
| -- Yeah. Boring =P
| |
| print(x4) -- Want to print the value of a variable? print(VARIABLE NAME HERE)
| |
| |output=
| |
| nil
| |
| |fit=output}}}}
| |
|
| |
| ===[[Boolean|Booleans]]===
| |
|
| |
| A [[boolean]] is a simple data type. It is either '''true''' or '''false'''—always. There are no exceptions. Here are some examples:
| |
| {{Example|{{Code and output|code=
| |
| bool = true
| |
| thisGuideIsntAwesome = false
| |
| thisGuideRocks = true
| |
| print(thisGuideRocks)
| |
| print(thisGuideIsntAwesome)
| |
| |output=
| |
| true
| |
| false
| |
| |fit=output}}}}
| |
|
| |
| ===[[Number|Numbers]]===
| |
|
| |
| A [[number]] is a data type that can hold a number. In Lua, unlike other languages, a number can transfer between any of the following types of numbers without any special functions:
| |
| *[[Integer]] (no decimal point: {{`|32}}
| |
| *Floating-point (have a decimal point: {{`|32.4}}
| |
| *[[Hexadecimal]] (I wouldn't recommend using that if you don't know what it does. Example: {{`|0xFFA3}}
| |
| Here are some examples:
| |
| {{Example|{{Code and output|code=
| |
| num1 = 9123
| |
| num2 = 1234122.1234
| |
| num3 = 0xAFB2
| |
| print(num1)
| |
| print(num2)
| |
| print(num3)
| |
| |output=
| |
| 9123
| |
| 1234122.1234
| |
| 44978
| |
| |fit=output}}}}
| |
|
| |
| ===[[String|Strings]]===
| |
|
| |
| A [[string]] is a data type that holds '''any''' {{`|characters}], with one exception: a single-line quote. Before we begin, a ''string literal'' is any string placed inside of what I will refer to as ''string marks'' in the following sections. When declaring a variable as a string, you will most likely declare it as a string literal.
| |
|
| |
| ====One-Line Strings====
| |
|
| |
| The ''string marks'' for a one-line string are quotation marks ({{`|" "}} or {{`|' '}}. Please use the double quotes, not the single quotes for strings. Here are some examples:
| |
| {{Example|{{Code and output|code=
| |
| name = "Merlin11188"
| |
| dateWhenWritten = "1/22/2012"
| |
| greeting = "Hi! How are you?"
| |
| print(name)
| |
| print(dateWhenWritten)
| |
| print(greeting)
| |
| |output=
| |
| Merlin11188
| |
| 1/22/2012
| |
| Hi! How are you?
| |
| |fit=output}}}}
| |
|
| |
| ====Multi-Line Strings====
| |
|
| |
| The ''string marks'' for a multi-line string are brackets ({{`|[[<--start. end -->]]}}). They can contain '''any and all''' {{`|characters}} Please ignore the syntax highlighting (color of the text) in the following example; it's inaccurate.
| |
| {{Example|{{Code and output|code=
| |
| longString = [[I'm a very
| |
| long string that
| |
| has multiple
| |
| lines and takes up
| |
| a lot of
| |
| space.
| |
| ]]
| |
| otherLongString = [[I'm multiple lines,
| |
| but still relatively short!]]
| |
| print(longString)
| |
| print(otherLongString)
| |
| |output=
| |
| I'm a very
| |
| long string that
| |
| has multiple
| |
| lines and takes up
| |
| a lot of
| |
| space.
| |
|
| |
| I'm multiple lines,
| |
| but still relatively short!
| |
| |fit=output}}}}
| |
|
| |
| Much like [[Comments#Leveled_Comments|leveled comments]], you may also have leveled strings. They work in the exact same way, except they don't start with {{`|--}}.
| |
|
| |
| ===Objects===
| |
|
| |
| Objects are more advanced data types, such as:
| |
| *Tables
| |
| *Functions
| |
| *Threads
| |
| *Userdatas
| |
| I'll go into those later.
| |
|
| |
| ==Expressions==
| |
|
| |
| An expression is a chunk in Lua that manipulations a value through the use of [[Operator|operators]].
| |
|
| |
| ===[[Operator|Operators]]===
| |
|
| |
| An ''[[operator]]'' allows you to manipulate the value of a variable. Some examples of manipulation are addition, subtraction, and combining two strings. Yay! Useful things!
| |
|
| |
| ====[[Arithmetic_Operators|Arithmetic]]====
| |
|
| |
| The mathematical operators are:
| |
| *{{`|+}}; to add two numbers
| |
| *{{`|-}}; to subtract two numbers (or to get the opposite of a number)
| |
| *{{`|*}}; to multiply two numbers
| |
| *{{`|/}}; to divide two numbers
| |
| *{{`|^}}; to raise a number to an exponent
| |
| *{{`|%}}; for the modulus of one number by another number
| |
|
| |
| The modulus operator returns the remainder of division. So, {{`|7 % 3}} equals 1 because {{`|<nowiki>7/3 = 2 r1</nowiki>}}. Here are some examples:
| |
| {{Example|{{Code and output|code=
| |
| x = 4
| |
| x = x*2
| |
| print(x)
| |
| x = x + 6
| |
| print(x)
| |
| x = x - 2
| |
| print(x)
| |
| x = x/3
| |
| print(x)
| |
| x = x^2
| |
| print(x)
| |
| x = x%5
| |
| print(x)
| |
| x = x/2
| |
| print(x)
| |
| |output=
| |
| 8
| |
| 14
| |
| 12
| |
| 4
| |
| 16
| |
| 1
| |
| 0.5
| |
| |fit=output}}}}
| |
|
| |
| ====[[Operator#Comparative|Relational]]====
| |
|
| |
| [[Operator#Comparative|Relational operators]] '''always''' return a ''[[boolean]]'', '''true''' or '''false'''. There are six relational operators:
| |
| *{{`|<nowiki>></nowiki>}}; greater than
| |
| *{{`|<nowiki><</nowiki>}}; less than
| |
| *{{`|<nowiki>>=</nowiki>}}; greater than or equal to
| |
| *{{`|<nowiki><=</nowiki>}}; less than or equal to
| |
| *{{`|<nowiki>==</nowiki>}}; equal to
| |
| *{{`|<nowiki>~=</nowiki>}}; not equal to
| |
|
| |
| Remember from math class what those things do? No? Fine, then. Here are examples:
| |
| {{Example|{{Code and output|code=
| |
| w, x, y, z = 3, 4, 5, 3
| |
| print(w > x)
| |
| print(x < y)
| |
| print(y <= z)
| |
| print(w >= z)
| |
| |output=
| |
| false
| |
| true
| |
| false
| |
| true
| |
| |fit=output}}}}
| |
|
| |
| ====[[Operator#Logical|Logical]]====
| |
|
| |
| All [[Operator#Logical|logical operators]] consider false and nil as false and anything else as true. There are three logical operators in Lua:
| |
| *[[And_operator|{{`|and}}]]
| |
| *[[Or_operator|{{`|or}}]]
| |
| *[[Not_operator|{{`|not}}]]
| |
|
| |
| =====[[And_operator|And]]=====
| |
|
| |
| The [[And_operator|{{`|and}} operator]] checks its first operand (the thing that comes before it); if its first operand is false or nil, {{`|and}} returns that operand's value. If its first operand is not false nor nil, then it returns the second operand's value (the value of the thing that comes after it). Here are some examples:
| |
|
| |
| {{Example|{{Code and output|code=
| |
| a, b, c, d = 1, true, false, "Hi"
| |
| print(a and b)
| |
| print(a and d)
| |
| print(b and c)
| |
| print(c and a)
| |
| print(b and d)
| |
| |output=
| |
| true
| |
| Hi
| |
| false
| |
| false
| |
| Hi
| |
| |fit=output}}}}
| |
|
| |
| =====[[Or_operator|Or]]=====
| |
| The [[Or_operator|{{`|or}} operator]] checks if its first operand is not nil nor false; if it's not nil nor false then it returns that operand's value. Otherwise, it returns the second operand's value. Here are some examples:
| |
|
| |
| {{Example|{{Code and output|code=
| |
| a, b, c, d = 1, true, false, "Hi"
| |
| print(a or b)
| |
| print(a or c)
| |
| print(a or d)
| |
| print(b or c)
| |
| print(b or d)
| |
| print(c or d)
| |
| print(c or a)
| |
| print(d or c)
| |
| print(c or c)
| |
| |output=
| |
| 1
| |
| 1
| |
| 1
| |
| true
| |
| true
| |
| Hi
| |
| 1
| |
| Hi
| |
| false
| |
| |fit=output}}}}
| |
|
| |
| =====Not=====
| |
|
| |
| The [[Not_operator|{{`|not}} operator]], unlike the other [[Operator#Logical|logical operators]] '''always''' returns a [[boolean]], '''true''' or '''false'''. if its ''only'' operand is nil or false, then true is returned. If its only operand is not nil nor false, then false is returned. Here are some examples:
| |
| {{Example|{{Code and output|code=
| |
| a, b, c, d = 1, true, false, "Hi"
| |
| print(not a)
| |
| print(not b)
| |
| print(not c)
| |
| print(not d)
| |
| |output=
| |
| false
| |
| false
| |
| true
| |
| false
| |
| |fit=output}}}}
| |
|
| |
| ====[[Concatenation]]====
| |
|
| |
| The [[Concatenation|concatenation operator]] ({{`|..}}) appends one string onto another. In the case of one of the operands being a number, converts it to a string first. Here are some examples:
| |
|
| |
| {{Example|{{Code and output|code=
| |
| x, y, z = "He", "llo ", "there."
| |
| print(x .. y)
| |
| print(x .. z)
| |
| print(y .. x)
| |
| print(x .. " who is happy is rich.")
| |
| print(z .. " he is!")
| |
| print(z .. 35)
| |
| print(35 .. 35)
| |
| |output=
| |
| Hello
| |
| Hethere.
| |
| llo He
| |
| He who is happy is rich.
| |
| there. he is!
| |
| there.35
| |
| 3535
| |
| |fit = output}}}}
| |
|
| |
| ===[[Precedence]]===
| |
| [[Precedence]] refers to the order in which operators are executed. When you have a math equation, like this:
| |
| <pre>
| |
| 3*4 + 2*9 - 7/9 % 3 + 4 * 5
| |
| </pre>
| |
| what order does Lua do that in? Does it work it from right to left? (Hint: The answer is 49.222222222222.) No. Lua doesn't. That's where precedence comes into play. Which one is evaluated first? Why, the operators are evaluated in the order from top to bottom of this chart:
| |
| <pre>
| |
| ^
| |
| not - (unary)
| |
| * / %
| |
| + -
| |
| ..
| |
| < > <= >= ~= ==
| |
| and
| |
| or
| |
| </pre>
| |
| '''Note:''' Things inside of parentheses are evaluated first. (7 + 3)*2 will be 20, whereas 7 + 3*2 will be 13.
| |
| Operators with the same precedence are evaluated from left to right. So, in the above equation, the first things to be evaluated is the 3*4, then the 2*9, then the 7/9, then the (7/9)%3, then the 4*5, and so on. As with all operators, you can put strings of operators together. Here are some examples:
| |
|
| |
| {{Example|{{Code and output|code=
| |
| print(3*4 + 9/3 - 6%2^3/3 + 9 - 8 .. " is a number.")
| |
| print(6^3*3-48 .. " is a multiple of 100 and is equal to " .. 3 .. "*" .. 4 .. "*" .. 50 .. ".")
| |
| |output=
| |
| 14 is a number.
| |
| 600 is a multiple of 100 and is equal to 3*4*50.
| |
| |fit=code}}}}
| |