Day Two - Values, Operators, and Assignments
Posted: Wed Apr 03, 2013 6:15 pm
Day Two - Values, Operators, and Assignments
(As always, if I make any mistakes, please correct me so I'm not misleading anyone else. I'm collating information from many different sources, some of which undoubtedly refer to older versions of lua):
Picture the process of making soup from scratch: You have three pans on the stove; one to sautee your onions, garlic and meat (or, in my case, veggie crumbles), one for your broth, and the last for your pasta. When everything is ready, you'll combine all three pans into one large pot, and there's your soup. Now if you can think about making a pot of soup in comparison to writing a Lua script, the script is the final pot (into which everything has been combined), the variables are the individual pans, and values are the ingredients in each pan. Assignment is the process of placing those values into the variables (or ingredients into pans).
Now values by themselves aren't very useful, in the same way that diced onion is just diced onion. To be useful, we place them with operators and once we've done that, they become operands (in the same way that once the onions have been paired with high heat, they become sauteed). Yum!
Confused? Don't be. Consider the following (if you don't have Lua compiled on your own system, you can start a new map in the editor and just place the following in a script_entity; when you press play it will execute and display the results in the upper left corner of the preview window).
In the above example, the value "45" is plugged into the variable "myNumber," using the operator "=." Since we've done that, we can now refer to "45" as the operand. This is also a good place to point out that Lua's equal sign (=) is a command to make two things equal, which differs slightly from the algebraic equal sign (as a statement of fact that two things are already equal). This means that we can easily change a variable, simply by changing the operand - even within the same chunk of code:
Executing this results in two different returns from Lua, even though there is only one variable used ("myNumber"):
45
263
Multiple values can be assigned to multiple variables on a single line, by using a comma:
When you execute that chunk, Lua will return the following:
45, 263, 308
Now, this does not mean that the operand to the right of an operator must always be a value. In fact, it can also be another variable:
Pretty nifty, yes?
Arithmetic Operators:
Let's go back to the chunk we saw above:
In order to get the sum of 45 and 263, we used an addition sign, which is an arithmetic operator. There are other arithmetic operators of course, and they are separated into two categories: binary (a calculation involving two operands) and unary (which involves only one operand). Can you guess what they all are?
Arithmetic Operator (Binary):
Arithmetic Operator (Unary) (Hint: There's only one!):
The modulo operator basically divides two numbers and ouputs the remainder. Examples of these arithmetic operators are:
Returns from Lua if executed:
Relational Operators:
While arithmetic operators deal with the manipulation of values, relational operators simply compare them. Because of this, all of these operators will produce a boolean result.
(Remember what boolean means?)
Relational operators are:
Returns from Lua if executed:
Note that while <, >, <=, and >= look at the order of the two values, == and ~= simply determine whether or not they are equal. Added by Ryeath_Greystalk : <= , >=, ~= are not reversible. Found this out the hard way. => or =< will get you an error.
Logical Operators:
Logical operators in Lua are: and, or, and not. Remember that Lua considers a value to always be true unless it is declared false or nil? We can see this in action with these three operators. All three can produce boolean results, but only one does so at all times, so we'll look at that one first.
not: As stated, the not operator will always result in either true or false. Placing two nots (not not) before a true value always results in true, while placing two nots before a false value always results in false:
Returns from Lua if executed:
and and or: These operators only look at their second operand when they need to, in order to avoid causing run-time errors (this is called short-cut evaluation, but it's not necessary you know this). And will return its first argument if the value is false or nil, while or will return its first argument if the value is not false or nil; otherwise, both operators will return their second argument. What this means is that the result is not always boolean:
Looking at the returns from Lua upon execution can help clarify these:
So, how can this be useful? Let's say you want to create a puzzle where two things need to be present on alcoves before a door opens. By using the and operator, Lua will look at the first operand, see if it exists (is true), then move on to check the second operand. If false, it will go no further; if true it will execute the rest of the chunk (open the door).
That's it for today. I hope you're finding these useful!
(As always, if I make any mistakes, please correct me so I'm not misleading anyone else. I'm collating information from many different sources, some of which undoubtedly refer to older versions of lua):
Picture the process of making soup from scratch: You have three pans on the stove; one to sautee your onions, garlic and meat (or, in my case, veggie crumbles), one for your broth, and the last for your pasta. When everything is ready, you'll combine all three pans into one large pot, and there's your soup. Now if you can think about making a pot of soup in comparison to writing a Lua script, the script is the final pot (into which everything has been combined), the variables are the individual pans, and values are the ingredients in each pan. Assignment is the process of placing those values into the variables (or ingredients into pans).
Now values by themselves aren't very useful, in the same way that diced onion is just diced onion. To be useful, we place them with operators and once we've done that, they become operands (in the same way that once the onions have been paired with high heat, they become sauteed). Yum!
Confused? Don't be. Consider the following (if you don't have Lua compiled on your own system, you can start a new map in the editor and just place the following in a script_entity; when you press play it will execute and display the results in the upper left corner of the preview window).
Code: Select all
myNumber = 45
print(myNumber)
Code: Select all
myNumber = 45
print(myNumber)
myNumber = 263
print(myNumber)
45
263
Multiple values can be assigned to multiple variables on a single line, by using a comma:
Code: Select all
myNumber, yourNumber, sumOfBothNumbers = 45, 263, 45 + 263
print(myNumber, yourNumber, sumOfBothNumbers)
45, 263, 308
Now, this does not mean that the operand to the right of an operator must always be a value. In fact, it can also be another variable:
Code: Select all
myNumber = 45 --variable = value
yourNumber = myNumber --variable = variable
print(yourNumber)
Arithmetic Operators:
Let's go back to the chunk we saw above:
Code: Select all
myNumber, yourNumber, sumOfBothNumbers = 45, 263, 45 + 263
print(myNumber, yourNumber, sumOfBothNumbers)
Arithmetic Operator (Binary):
SpoilerShow
They are + (addition), - (subtraction), * (multiplication), / (division), % (modulo), and ^ (exponentiation)
SpoilerShow
It's - (the same subtraction operator as used in binary , but used to negate a numeric value, such as subtracting a value from 0)
Code: Select all
print(2 + 2)
print(4 - 2)
print(2 * 2)
print(4 / 2)
print(10 % 3) --divides 10 by 3 and returns the remainder
print(6 ^ 4) --this is the same as 6 * 6 * 6 * 6
print(-(-15)) --note that negating a negative number returns a positive
SpoilerShow
4
2
4
2
1
1296
15
2
4
2
1
1296
15
While arithmetic operators deal with the manipulation of values, relational operators simply compare them. Because of this, all of these operators will produce a boolean result.
(Remember what boolean means?)
SpoilerShow
Boolean values are either true or false.
- < less than
> greater than
== equal to
~= not equal to
<= less than or equal to
>= greater than or equal to
Code: Select all
print(3 < 4)
print(3 > 4)
print(3 == 4)
print(3 ~= 4)
print(3 <= 4)
print(3 >= 4)
SpoilerShow
true
false
false
true
true
false
false
false
true
true
false
Logical Operators:
Logical operators in Lua are: and, or, and not. Remember that Lua considers a value to always be true unless it is declared false or nil? We can see this in action with these three operators. All three can produce boolean results, but only one does so at all times, so we'll look at that one first.
not: As stated, the not operator will always result in either true or false. Placing two nots (not not) before a true value always results in true, while placing two nots before a false value always results in false:
Code: Select all
print(not true)
print(not false)
print(not nil)
print(not not true)
print(not not false)
print(not not nil)
print(not "Grimrock is amazing!")
SpoilerShow
false
true
true
true
false
false
false --because Grimrock really is amazing!
true
true
true
false
false
false --because Grimrock really is amazing!
Code: Select all
print(8 and 15)
print(8 or 15)
print(nil and 8)
print(nil or 8)
print(true and false)
print(true or false)
SpoilerShow
15 --and looks at the first argument, sees it as true, and returns the second
8 --or looks at the first argument, sees it as true, and returns it without looking at the second argument
nil --put another way, if the operand to the left of and is false, the result is always false
8
false
true --put another way, if the operand to the left of or is true the result is always true
8 --or looks at the first argument, sees it as true, and returns it without looking at the second argument
nil --put another way, if the operand to the left of and is false, the result is always false
8
false
true --put another way, if the operand to the left of or is true the result is always true
That's it for today. I hope you're finding these useful!