(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):
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
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?)
- < 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)
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!")
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)
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!