Difference between revisions of "Basic lua: Operators"
From The Official Visionaire Studio: Adventure Game Engine Wiki
m |
m |
||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | |||
− | |||
{| class="in" | {| class="in" | ||
|- | |- | ||
− | | class="i_arrow clickablecell" | [[basic_lua: | + | | class="i_arrow clickablecell" | [[basic_lua:_Basics|<]] |
| class="i_norm clickablecell" | '''[[basic_lua:_Index|Index]]''' | | class="i_norm clickablecell" | '''[[basic_lua:_Index|Index]]''' | ||
| class="i_arrow clickablecell" | [[basic_lua:_Types|>]] | | class="i_arrow clickablecell" | [[basic_lua:_Types|>]] | ||
|} | |} | ||
+ | <hr> | ||
+ | Lua operators are expressions used to perform calculations or to pass arguments between different value types. | ||
== Conditional Operators == | == Conditional Operators == | ||
Line 33: | Line 33: | ||
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(conditional_operators)_1.png|frameless|center|180px]] | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(conditional_operators)_1.png|frameless|center|180px]] | ||
|} | |} | ||
− | + | <hr> | |
{| class="ts" | {| class="ts" | ||
|- | |- | ||
Line 46: | Line 46: | ||
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(conditional_operators)_2.png|frameless|center|180px]] | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(conditional_operators)_2.png|frameless|center|180px]] | ||
|} | |} | ||
− | + | <hr> | |
{| class="ts" | {| class="ts" | ||
|- | |- | ||
Line 172: | Line 172: | ||
print("a = 1") | print("a = 1") | ||
end | end | ||
− | </syntaxhighlight> || width=" | + | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(comparison_operators)_1.png|frameless|center|180px]] |
|} | |} | ||
Line 186: | Line 186: | ||
print("a = " .. a) | print("a = " .. a) | ||
end | end | ||
− | </syntaxhighlight> || width=" | + | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(comparison_operators)_2.png|frameless|center|180px]] |
|} | |} | ||
Line 200: | Line 200: | ||
print("a = " .. a) | print("a = " .. a) | ||
end | end | ||
− | </syntaxhighlight> || width=" | + | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(comparison_operators)_3.png|frameless|center|180px]] |
|} | |} | ||
Line 214: | Line 214: | ||
print("a = " .. a) | print("a = " .. a) | ||
end | end | ||
− | </syntaxhighlight> || width=" | + | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(comparison_operators)_4.png|frameless|center|180px]] |
|} | |} | ||
Line 228: | Line 228: | ||
print("a = " .. a) | print("a = " .. a) | ||
end | end | ||
− | </syntaxhighlight> || width=" | + | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(comparison_operators)_5.png|frameless|center|180px]] |
|} | |} | ||
Line 242: | Line 242: | ||
print("a = " .. a) | print("a = " .. a) | ||
end | end | ||
− | </syntaxhighlight> || width=" | + | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(comparison_operators)_6.png|frameless|center|180px]] |
|} | |} | ||
Line 272: | Line 272: | ||
print( result ) | print( result ) | ||
− | </syntaxhighlight> || width=" | + | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(mathematical_operators)_1.png|frameless|center|180px]] |
|} | |} | ||
Line 286: | Line 286: | ||
print( result ) | print( result ) | ||
− | </syntaxhighlight> || width=" | + | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(mathematical_operators)_2.png|frameless|center|180px]] |
|} | |} | ||
Line 299: | Line 299: | ||
print( "original value: " .. a, "inversed value: " .. result ) | print( "original value: " .. a, "inversed value: " .. result ) | ||
− | </syntaxhighlight> || width=" | + | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(mathematical_operators)_3.png|frameless|center|180px]] |
|} | |} | ||
Line 312: | Line 312: | ||
print( result ) | print( result ) | ||
− | </syntaxhighlight> || width=" | + | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(mathematical_operators)_4.png|frameless|center|180px]] |
|} | |} | ||
Line 326: | Line 326: | ||
print( result ) | print( result ) | ||
− | </syntaxhighlight> || width=" | + | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(mathematical_operators)_5.png|frameless|center|180px]] |
|} | |} | ||
Line 332: | Line 332: | ||
{| class="ts" | {| class="ts" | ||
|- | |- | ||
− | ! base of 2, power of 8 (2ⁿ) = 256 | + | ! base of 2, power of 8 (2ⁿ) = 256 (2x2=4x2=8x2=16x2=32x2=64x2=128x2=256) !! |
|- | |- | ||
| <syntaxhighlight lang="lua"> | | <syntaxhighlight lang="lua"> | ||
− | local a, b = 2, | + | local a, b = 2, 8 |
local result = a ^ b -- generate answer | local result = a ^ b -- generate answer | ||
− | + | ||
-- break down power function into string result (ignore this code) | -- break down power function into string result (ignore this code) | ||
function power(i1, i2) | function power(i1, i2) | ||
Line 347: | Line 347: | ||
return "which is the equivalent of " .. val | return "which is the equivalent of " .. val | ||
end | end | ||
− | + | ||
− | print("power value of "..a.." & "..b.." | + | print("power value of "..a.." & "..b.." = " .. result, power(a,b)) |
− | </syntaxhighlight> || width=" | + | |
+ | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(mathematical_operators)_6.png|frameless|center|180px]] | ||
|} | |} | ||
Line 355: | Line 356: | ||
{| class="ts" | {| class="ts" | ||
|- | |- | ||
− | ! remainder of 25 ÷ 4 = 1 !! | + | ! remainder of 25 ÷ 4 = 1 (25 ÷ 4 = 6.25 - 6 = 0.25 * 4 = 1) !! |
|- | |- | ||
− | | <syntaxhighlight lang="lua"> | + | | style="max-width:680px;" | <syntaxhighlight lang="lua"> |
local a = 25 | local a = 25 | ||
local b = 4 | local b = 4 | ||
− | local result = a / 4 | + | local result = a / b -- 25 ÷ 4 = 6.25 |
− | local remainder = a % b | + | local result_decimal = math.fmod(result, 1) -- 6.25 - 6 = 0.25 |
− | + | local result_rounded = math.modf(result) -- 6.25 - 0.25 = 6 | |
+ | local remainder = a % b -- 0.25 * 4 = 1 | ||
− | print("result: | + | print("result: " .. result .. ", decimal: " .. result_decimal .. ", rounded: " .. result_rounded .. ", remainder: " .. remainder) |
− | </syntaxhighlight> || width=" | + | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(mathematical_operators)_7.png|frameless|center|180px]] |
|} | |} | ||
− | + | <hr> | |
{| class="in" | {| class="in" | ||
|- | |- | ||
− | | class="i_arrow" | [[basic_lua: | + | | class="i_arrow" | [[basic_lua:_Basics|<]] |
| class="i_norm" | '''[[basic_lua:_Index|Index]]''' | | class="i_norm" | '''[[basic_lua:_Index|Index]]''' | ||
| class="i_arrow" | [[basic_lua:_Types|>]] | | class="i_arrow" | [[basic_lua:_Types|>]] | ||
|}{{toc}} | |}{{toc}} |
Latest revision as of 13:50, 5 September 2022
< | Index | > |
Lua operators are expressions used to perform calculations or to pass arguments between different value types.
Conditional Operators
if | Query if something does or does not meet a certain condition. |
else | Do something else if the query condition was not met. |
elseif | Used to add additional if queries, if the initial query condition was not met. |
end | This is used to close various queries or functions; there must be the same amount of end as if; except in instances where elseif has been used. |
if
if condition is true (method 1) | |
---|---|
local a = true
if a then
print("a = true")
end
|
if condition is true (method 2) | |
---|---|
local a = true
if a == true then
print("a = true")
end
|
if condition is false | |
---|---|
local a = false
if a == false then
print("a = false")
end
|
else
if condition is not met, then do else | |
---|---|
local a = false
if a then
print("a = true")
else
print("a = false")
end
|
elseif
if condition is not met, then do elseif | |
---|---|
local a = false
if a then
print("a = true")
elseif a == false then
print("a = false")
end
|
Logical Operators
and | Allows you to check multiple conditions in a single if query. |
or | Allows you to add an alternative if query into a single if query. |
not | Checks if condition equals false or negative. |
and
if condition a is true and condition b is false then | |
---|---|
local a = true
local b = false
if a == true and b == false then
print("a = true", "b = false")
end
|
or
if condition is true or condition == true then | |
---|---|
local a = true
if a or a == true then
print("a = true")
end
|
not
if condition is not true | |
---|---|
local a = false
if not a then
print("a = false")
end
|
Comparison Operators
== | Equal to. |
~= | Does not equal. |
>= | Greater than or equal to. |
> | Greater than. |
<= | Less than or equal to. |
< | Less than. |
equal to
if a equals 1 | |
---|---|
local a = 1
if a == 1 then
print("a = 1")
end
|
does not equal
if a does not equal 2 | |
---|---|
local a = 1
if a ~= 2 then
print("a = " .. a)
end
|
greater than or equal to
if a is greater than or equal to 2 | |
---|---|
local a = 5
if a >= 2 then
print("a = " .. a)
end
|
greater than
if a is greater than 2 | |
---|---|
local a = 3
if a > 2 then
print("a = " .. a)
end
|
less than or equal to
if a less than or equal to 2 | |
---|---|
local a = 2
if a <= 2 then
print("a = " .. a)
end
|
less than
if a less than 2 | |
---|---|
local a = 0
if a < 2 then
print("a = " .. a)
end
|
Mathematical Operators
+ | Add |
- | Subtract or invert |
* | Multiply |
/ | Divide |
^ | Power |
% | Remainder |
add
1 + 2 = 3 | |
---|---|
local a = 1
local b = 2
local result = a + b
print( result )
|
subtract
3 - 2 = 1 | |
---|---|
local a = 3
local b = 2
local result = a - b
print( result )
|
inverse
inv of 3 = -3 | |
---|---|
local a = 3
local result = -a
print( "original value: " .. a, "inversed value: " .. result )
|
multiply
2 x 2 = 4 | |
---|---|
local a = 2
local result = a * a
print( result )
|
divide
10 ÷ 5 = 2 | |
---|---|
local a = 10
local b = 5
local result = a / b
print( result )
|
power
base of 2, power of 8 (2ⁿ) = 256 (2x2=4x2=8x2=16x2=32x2=64x2=128x2=256) | |
---|---|
local a, b = 2, 8
local result = a ^ b -- generate answer
-- break down power function into string result (ignore this code)
function power(i1, i2)
local val = "" -- clear string
--+--
for i = 1, i2 do -- generate the longhand math formula
if i < i2 then val = val..i1.."*" else val = val..i1 end
end
return "which is the equivalent of " .. val
end
print("power value of "..a.." & "..b.." = " .. result, power(a,b))
|
remainder
remainder of 25 ÷ 4 = 1 (25 ÷ 4 = 6.25 - 6 = 0.25 * 4 = 1) | |
---|---|
local a = 25
local b = 4
local result = a / b -- 25 ÷ 4 = 6.25
local result_decimal = math.fmod(result, 1) -- 6.25 - 6 = 0.25
local result_rounded = math.modf(result) -- 6.25 - 0.25 = 6
local remainder = a % b -- 0.25 * 4 = 1
print("result: " .. result .. ", decimal: " .. result_decimal .. ", rounded: " .. result_rounded .. ", remainder: " .. remainder)
|
< | Index | > |