Okay... So I did not quite like these other beginner tutorials around here as I don't think they really describe things how it really works and not good enough.
So this will be my attempt to create a more understandable tutorial that will cover pretty much everything you will need to know to start off scripting in Lua.
Operators
Arithmetic Operators
Unlike other programming/scripting languages Lua does not support incremental/decremental operators such as "i++" or "i--".
The following code will not run in Lua:
We will look further into variables soon, so don't worry if you didn't catch!
I'll go through each arithmetic operator in below script:
Relational Operators
A statement is when you have 2 values and compare them to each other using either of the listed operators, the result is a boolean value, either true or false depending on whether the statement was succeeded or not. Here is an example of the usage:
Note: Be cautious for not mixing "==" with "="
Relational operators are used in if-statements, which we will go through in this tutorial. They could also be used for different purposes, but that is irrelevant for now.
Logical Operators
Logical operators combines with relational operators.
If-statements
The structure of an if-statement is the following:
An if-statement will go through only if the statement returns "true"! If it is not true, then the block-code (code within the if-statement) will not be ran!
Now as that statement is true already, it will always run. However if it was "false" it would never run. An if-statement normally contains relational and logical operators.
For more complicated statements the use of parenthesis could be very handy.
Tutorial is incomplete and will be filled out as quick as possible!
For further learning check out the tutorials linked in my signature below.
So this will be my attempt to create a more understandable tutorial that will cover pretty much everything you will need to know to start off scripting in Lua.
- Operators
- If-statements
- Variables & Scopes
Operators
Arithmetic Operators
- + (addition)
- - (subtraction)
- * (multiplication)
- / (division)
- ^ (exponential)
- - (negation)
Unlike other programming/scripting languages Lua does not support incremental/decremental operators such as "i++" or "i--".
The following code will not run in Lua:
Lua:
i = 10
i++; -- This is not allowed in Lua, and will return an error in your console.
i = i + 1 -- This is the valid version of above.
i += 5 -- This is not allowed in Lua, and will return an error in your console.
i = i + 5 -- This is the valid version of above.
I'll go through each arithmetic operator in below script:
Lua:
a = 2 + 5 -- The result is 7, regular addition method.
a = -4 + 6 -- Result is 2 (Would be same if it was "6 + -4").
a = 10 - 4 -- Result is 6, regular subtraction method.
a = 4 - 7 -- Result is -3
a = 5 * 3 -- Result is 15, regular multiplication method.
a = -3 * 3 -- Result is -9
a = 20 / 4 -- Result is 5, regular division method.
a = 20 / -5 -- Result is -4
a = 5 ^ 3 -- Result is 125 (5 * 5 * 5), regular exponential method
a = -10 ^ 2 -- Result is -100
a = 10 ^ -2 -- Result is 0.01
a = -4 -- Negation (note the "-" token).
Relational Operators
- == (a is equal to b)
- ~= (a is not equal to b)
- < (a is less than b)
- > (a is greater than b)
- <= (a is less or equal to b)
- >= (a is greater or equal to b)
A statement is when you have 2 values and compare them to each other using either of the listed operators, the result is a boolean value, either true or false depending on whether the statement was succeeded or not. Here is an example of the usage:
Lua:
4 == 6 -- returns false, because obviously 4 does not equal to 6
4 == 4 -- returns true, 4 equals to 4
3 ~= 5 -- returns true, because 4 does not equal to 6 (which is what we want)
3 ~= 3 -- returns false, 4 equals to 4 (we don't want them to be equal)
7 < 3 -- returns false, 7 is not less than 3
2 < 3 -- returns true, 2 is less than 3
3 < 3 -- returns false, 3 is not less than 3
8 > 4 -- returns true, 8 is greater than 4
4 > 4 -- returns false, 4 is not greater than 4
2 > 4 -- returns false, 2 is not greater than 4
6 <= 8 -- returns true, 6 is not equal, but less than 8
8 <= 8 -- returns true, 8 is not less, but equal to 8
12 <= 8 -- returns false, 12 is not less or equal to 8
1 >= 5 -- returns false, 1 is not greater or equal to 5
9 >= 5 -- returns true, 9 is not equal, but greater than 5
5 >= 5 -- returns true, 5 is not greater, but equal to 5
Note: Be cautious for not mixing "==" with "="
Relational operators are used in if-statements, which we will go through in this tutorial. They could also be used for different purposes, but that is irrelevant for now.
Logical Operators
- and
- or
- not
Logical operators combines with relational operators.
Lua:
10 < 5 and 3 < 9
-- to make it easy for you, we'll convert them one after one...
-- 10 < 5, is false so now we have:
false and 3 < 9
-- 3 < 9 is true, so now we have:
false and true
-- Because we have "false and true", this whole statement will be false. Because the statement wants them BOTH to be true.
false
-- However, if we do the complete same thing, just with "or" it will look like this instead:
10 < 5 or 3 < 9 -- Doing the same as we did before, I'll skip the steps you already know.
false or true -- As before, we have the same values, except this time it's "or" instead of "and"
-- Now it's "false or true", which means it will only want one of them to be true for the statement to go through, "either a is true or b is true" while the and statement says "a is true and b is true". Our result will be:
true
-- To repeat, the reason of this is because it wants either one of them to be true. So if one is false, and one is true it's a true statement, if they both were true it's a true statement, however if they both where false it would be a false statement.
10 < 5 and not 3 < 9
-- The "not" operator simply inverts the statement after it. If "3 < 9" is true, the "not" operator will turn it into false, and vice versa. So the above statement equals to:
10 < 5 and 3 >= 9
-- To slow it down a bit, we know that 3 < 9 is true, so we can replace it:
10 < 5 and not true
-- Now the "not" operator will invert "true" into "false" simply, and now we'd have:
10 < 5 and false
-- 10 < 5 is false so we would now have:
false and false
-- The end result would be:
false
If-statements
The structure of an if-statement is the following:
Lua:
if statement then -- statement opens with "if", and ends with "then"
-- here is the code that will be run if statement returns true
end -- close the if-statement with "end"
An if-statement will go through only if the statement returns "true"! If it is not true, then the block-code (code within the if-statement) will not be ran!
Lua:
if true then
-- block-code
end
Now as that statement is true already, it will always run. However if it was "false" it would never run. An if-statement normally contains relational and logical operators.
Lua:
if 14 < 10 then
-- 14 < 10 is false, therefore this code will not run.
end
if 3 < 7 then
-- 3 < 7 is true, the code will run.
end
if 3 == 5 or not 4 ~= 4 then
-- 3 == 5 is false, 4 ~= 4 is false, however "not" will inverse it, so we will have "false or true". Which means the result will be true and this code will run.
end
For more complicated statements the use of parenthesis could be very handy.
Tutorial is incomplete and will be filled out as quick as possible!
For further learning check out the tutorials linked in my signature below.
Last edited: