Difference between revisions of "Basic lua: Operators"
From The Official Visionaire Studio: Adventure Game Engine Wiki
m |
|||
Line 11: | Line 11: | ||
{| class="ts" | {| class="ts" | ||
|- | |- | ||
− | | | + | | <span class="vsyellow">if|if</span> || Query if something does or does not meet a certain condition. |
|- | |- | ||
− | | | + | | <span class="vsyellow">else|else</span> || Do something else if the query condition was not met. |
|- | |- | ||
− | | | + | | <span class="vsyellow">elseif</span> || Used to add additional if queries, if the initial query condition was not met. |
|- | |- | ||
| <span class="vsyellow">end</span> || 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. | | <span class="vsyellow">end</span> || 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. | ||
Line 95: | Line 95: | ||
{| class="ts" | {| class="ts" | ||
|- | |- | ||
− | | | + | | <span class="vsyellow">and</span> || width="90%" | Allows you to check multiple conditions in a single if query. |
|- | |- | ||
− | | | + | | <span class="vsyellow">or</span> || Allows you to add an alternative if query into a single if query. |
|- | |- | ||
− | | | + | | <span class="vsyellow">not</span> || Checks if condition equals false or negative. |
|} | |} | ||
Line 148: | Line 148: | ||
{| class="ts" | {| class="ts" | ||
|- | |- | ||
− | | < | + | | <span class="vsyellow">==</span> || width="90%" | Equal to. |
|- | |- | ||
− | | < | + | | <span class="vsyellow">~=</span> || Does not equal. |
|- | |- | ||
− | | < | + | | <span class="vsyellow">>=</span> || Greater than or equal to. |
|- | |- | ||
− | | < | + | | <span class="vsyellow">></span> || Greater than. |
|- | |- | ||
− | | < | + | | <span class="vsyellow"><=</span> || Less than or equal to. |
|- | |- | ||
− | | < | + | | <span class="vsyellow"><</span> || Less than. |
|} | |} | ||
Line 166: | Line 166: | ||
! if a equals 1 !! | ! if a equals 1 !! | ||
|- | |- | ||
− | | <syntaxhighlight> | + | | <syntaxhighlight lang="lua"> |
local a = 1 | local a = 1 | ||
Line 180: | Line 180: | ||
! if a does not equal 2 !! | ! if a does not equal 2 !! | ||
|- | |- | ||
− | | <syntaxhighlight> | + | | <syntaxhighlight lang="lua"> |
local a = 1 | local a = 1 | ||
Line 194: | Line 194: | ||
! if a is greater than or equal to 2 !! | ! if a is greater than or equal to 2 !! | ||
|- | |- | ||
− | | <syntaxhighlight> | + | | <syntaxhighlight lang="lua"> |
local a = 5 | local a = 5 | ||
Line 208: | Line 208: | ||
! if a is greater than 2 !! | ! if a is greater than 2 !! | ||
|- | |- | ||
− | | <syntaxhighlight> | + | | <syntaxhighlight lang="lua"> |
local a = 3 | local a = 3 | ||
Line 222: | Line 222: | ||
! if a less than or equal to 2 !! | ! if a less than or equal to 2 !! | ||
|- | |- | ||
− | | <syntaxhighlight> | + | | <syntaxhighlight lang="lua"> |
local a = 2 | local a = 2 | ||
Line 236: | Line 236: | ||
! if a less than 2 !! | ! if a less than 2 !! | ||
|- | |- | ||
− | | <syntaxhighlight> | + | | <syntaxhighlight lang="lua"> |
local a = 0 | local a = 0 | ||
Line 248: | Line 248: | ||
{| class="ts" | {| class="ts" | ||
|- | |- | ||
− | | < | + | | <span class="vsyellow">+</span> || width="90%" | Add |
|- | |- | ||
− | | < | + | | <span class="vsyellow">-</span> || Subtract or invert |
|- | |- | ||
− | | < | + | | <span class="vsyellow">*</span> || Multiply |
|- | |- | ||
− | | < | + | | <span class="vsyellow">/</span> || Divide |
|- | |- | ||
− | | < | + | | <span class="vsyellow">^</span> || Power |
|- | |- | ||
− | | < | + | | <span class="vsyellow">%</span> || Remainder |
|} | |} | ||
Line 266: | Line 266: | ||
! 1 + 2 = 3 !! | ! 1 + 2 = 3 !! | ||
|- | |- | ||
− | | <syntaxhighlight> | + | | <syntaxhighlight lang="lua"> |
local a = 1 | local a = 1 | ||
local b = 2 | local b = 2 | ||
Line 280: | Line 280: | ||
! 3 - 2 = 1 !! | ! 3 - 2 = 1 !! | ||
|- | |- | ||
− | | <syntaxhighlight> | + | | <syntaxhighlight lang="lua"> |
local a = 3 | local a = 3 | ||
local b = 2 | local b = 2 | ||
Line 294: | Line 294: | ||
! ''<small>inv</small>'' of 3 = -3 !! | ! ''<small>inv</small>'' of 3 = -3 !! | ||
|- | |- | ||
− | | <syntaxhighlight> | + | | <syntaxhighlight lang="lua"> |
local a = 3 | local a = 3 | ||
local result = -a | local result = -a | ||
Line 307: | Line 307: | ||
! 2 x 2 = 4 !! | ! 2 x 2 = 4 !! | ||
|- | |- | ||
− | | <syntaxhighlight> | + | | <syntaxhighlight lang="lua"> |
local a = 2 | local a = 2 | ||
local result = a * a | local result = a * a | ||
Line 320: | Line 320: | ||
! 10 ÷ 5 = 2 !! | ! 10 ÷ 5 = 2 !! | ||
|- | |- | ||
− | | <syntaxhighlight> | + | | <syntaxhighlight lang="lua"> |
local a = 10 | local a = 10 | ||
local b = 5 | local b = 5 | ||
Line 334: | Line 334: | ||
! base of 2, power of 8 (2ⁿ) = 256 !! | ! base of 2, power of 8 (2ⁿ) = 256 !! | ||
|- | |- | ||
− | | <syntaxhighlight> | + | | <syntaxhighlight lang="lua"> |
local a, b = 2, 16 | local a, b = 2, 16 | ||
local result = a ^ b -- generate answer | local result = a ^ b -- generate answer | ||
Line 357: | Line 357: | ||
! remainder of 25 ÷ 4 = 1 !! | ! remainder of 25 ÷ 4 = 1 !! | ||
|- | |- | ||
− | | | + | | <syntaxhighlight lang="lua"> |
local a = 25 | local a = 25 | ||
local b = 4 | local b = 4 |
Revision as of 16:08, 2 September 2022
Lua operators are expressions used to perform calculations or to pass arguments between different value types.
< | Index | > |
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 | |
---|---|
local a, b = 2, 16
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 | |
---|---|
local a = 25
local b = 4
local result = a / 4
local remainder = a % b
local decimal = remainder / b
print("result: 25 ÷ 4 = " .. result, "remainder: .25 x 4 = " .. remainder, "decimal: 1 ÷ 4 = " .. decimal )
|
< | Index | > |