User:Merlin11188/Sandbox: Difference between revisions
m Text replacement - "<SyntaxHighlight code="lua">" to "<syntaxhighlight lang="lua">" |
m Text replacement - "</SyntaxHighlight>" to "</syntaxhighlight>" Tags: mobile web edit mobile edit |
||
Line 82: | Line 82: | ||
aVariable = 6 | aVariable = 6 | ||
iHave134141NumbersInMyNameAndABooleanValue = true | iHave134141NumbersInMyNameAndABooleanValue = true | ||
</ | </syntaxhighlight>}} | ||
Remember that saying {{`|<nowiki>variable = someValue</nowiki>}} is called ''declaring a variable''. | Remember that saying {{`|<nowiki>variable = someValue</nowiki>}} is called ''declaring a variable''. | ||
Line 443: | Line 443: | ||
-- Do other stuff | -- Do other stuff | ||
end -- End the new code block. If condition is not truthy, then whatever is after this end is run. | end -- End the new code block. If condition is not truthy, then whatever is after this end is run. | ||
</ | </syntaxhighlight> | ||
In the above, if {{`|condition}} is a ''truthy'' value (neither nil nor false), then what is in the code block is run. Else, whatever is after the end is run (in this case, nothing). Now, let's make condition an actual value and throw in some more stuff: | In the above, if {{`|condition}} is a ''truthy'' value (neither nil nor false), then what is in the code block is run. Else, whatever is after the end is run (in this case, nothing). Now, let's make condition an actual value and throw in some more stuff: |
Latest revision as of 06:12, 27 April 2023
Do not edit! The creator of this subpage does not want it to be edited without permission. Please discuss any changes that you think are relevant on the talk page. |
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. Here it is in Lua:
Ta-da! For now and the rest of the tutorial, that box on the right is the output. Keep that in mind. 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:
Beginning
Terminology
There are four terms I want you to know before we continue:
- code
- Anything written in Lua.
- chunk
- One line of code in Lua.
- script
- Multiple chunks that accomplish a specific task in Lua.
- character
- Any of the ASCII values (numbers, letters, spaces, enter key, down arrow, etc).
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.
Short Comments
A one-line comment is made with --. It lasts for the rest of the line. Here are some examples:
Long Comments
A multi-line comment starts with --[[ and ends with ]]. Everything between the opening and the closing is treated as a comment, even if the comment is only in the middle of the line.
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:
There are also 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.
Variables
A variable is just a container. It holds a value. For a good understanding, please read the page on variables from sections 1 to 3.1, ignoring the section on Scope. We'll get to that later. Here are just some examples of variables:
x = 3
y = 4
aVariable = 6
iHave134141NumbersInMyNameAndABooleanValue = true
Remember that saying variable = someValue is called declaring a variable.
Types and Values
Nil
For our intents and purposes, nil means nothing. If you really want an in-depth guide, read the page on nil. Here are some examples:
Booleans
A boolean is a simple data type. It is either true or false—always. There are no exceptions. Here are some examples:
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:
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:
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.
Much like 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 operators.
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
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 7/3 = 2 r1. Here are some examples:
Relational
Relational operators always return a boolean, true or false. There are six relational operators:
- >; greater than
- <; less than
- >=; greater than or equal to
- <=; less than or equal to
- ==; equal to
- ~=; not equal to
Remember from math class what those things do? No? Fine, then. Here are examples:
Logical
All logical operators consider false and nil as false and anything else as true. There are three logical operators in Lua:
And
The 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:
Or
The 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:
Not
The not operator, unlike the other 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:
Concatenation
The 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:
Precedence
Precedence refers to the order in which operators are executed. When you have a math equation, like this:
3*4 + 2*9 - 7/9 % 3 + 4 * 5
what order does Lua do that in? Does it work it from right to left? (Hint: The result 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:
^ not - (unary) * / % + - .. < > <= >= ~= == and or
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:
Statements
In Lua, statements include assigning a value to a variable, control structures (which are talked about in this section), and function calls.
Variables, Scope, and Blocks
If you remember that section on variable scopes I told you not to read from earlier, you may recognize that the scopes page is that section. I think it does an excellent job of explaining, particularly the pictures. See the actual scope page for a more in-depth guide.
You should almost always use local variables. They are faster for more efficiency and it's easier to see where people are declaring them. Unless I have a good reason not to (I want a variable to be in the global scope), I will use local variables for the rest of this guide/reference. Here are some examples with scope:
Control Structures
Control structures are pieces of code that tell Lua what to do next. Normally, it goes from top to bottom, but that's what these are: they allow for the order to change. There are four basic types of control structures and one keyword:
- if <condition> then statements
- while <condition> do loops
- repeat <code> until <condition> loops
- for <var>=<start>, <end>, <increment> do loops
- The break keyword
if <condition> then statements
The most basic type of control structure is an if then statement. It's fairly self-explanatory: If the condition is true, then do stuff. If it isn't, then skip around. Here is the basic syntax for an if then statement:
if condition then -- Create a new code block
print("Hi")
print("Condition is truthy!")
-- Do other stuff
end -- End the new code block. If condition is not truthy, then whatever is after this end is run.
In the above, if condition is a truthy value (neither nil nor false), then what is in the code block is run. Else, whatever is after the end is run (in this case, nothing). Now, let's make condition an actual value and throw in some more stuff:
Remember that the relational operators always return a boolean? Saying if y > x then is the same as saying if 5 > 3 then, which evaluates to be true. But what if y is not greater than x? Then we have this scenario:
Because 3 is not greater than 5, the code that said print(y .. " > " .. x) was never run. It was skipped right over, as was everything all the way down to the end.
Else statement
Note: You do not need an end for an else statement. You need it only for the if <condition> then statement.
The else statement is an add-on to the if <condition> then statement. The else statement specifies what happens when <condition> in if <condition> then statements is false. If an else is present, and the condition evaluates to be false, then, rather than skip down to the end, the if then statement skips to the else. Here is an example, using similar code to the above::
y is 3 and x is 5; therefore, if 3 > 5 then evaluates to be false, and it runs the code after the else up until the end.
elesif <condition> then statements
An elseif statement is exactly what it sounds like: a combination of the if then statement and else statement. It is a statement that is evaluated if the original if statement evaluates to false. Like the else statement, it does not need an end; however, unlike the else statement, you attach a condition to it. Here is an example:
Notice how only the {{{1}}} got printed, not the 3 < 5. That's because once an if … elseif … else statement has found a condition that evaluates to be true (in this case, does 5 == 5), it stops evaluating and just jumps straight down to the end. The elseif y < x then statement wasn't even evaluated.