Basics: Operators

Operators for numbers and strings


Table of Contents

Comparison

~= (not equal to): for numbers, booleans, strings
== (equal to): for numbers, booleans, strings

object.isEqualTo ( hObject1, hObject2 ): for objects.
This is required to compare object handlers because because two handlers having different values (== operator returns false) may point to the same scene object.

Concatenation

String concatenation: .. (two dots)
Concatenate a number to a string is possible, but not the opposite.

local sString = "Hello ".."John"
-- sString is "Hello John" 
local nIndex = 3
local sString = "Monster "..nIndex
-- sString is "Monster 3"

Number Operators

Greater than: >
ex: if (i > 2) then

Less than: ex: if (i
Greater or equal: >=
ex: if (i >= 5) then

Less or equal: ex: if (i
Not equal: ~=
ex: if (i ~= 3) then

Add: +
ex: local nSum = nVar1 + nVar2

Substract: -
ex: local nSub = nVar1 - nVar2

Multiply: *
ex: local nProd = nVar1 * nVar2

Divide: /
ex: local nDiv = nVar1 / nVar2

Logic Operators

AND operator: and
ex: if ((hObj ~= nil) and (i ~= 3)) then

OR operator: or
ex: if ((hTarget1 ~= nil) or (hTarget2 ~= nil)) then

NOT operator: ~ (tilde)
ex: if (hObj ~= nil) then

Protect conditions to avoid operator priority problems with round brackets: ( )