Hello, I have a question. What does this do? ~=
Looks like an arithmetic operator, but not sure.
I've seen it used like this before.
local exampleVarriable = "Example" if exampleVarriable ~= nil then print("Something...") end
The ~= symbol means not equal
So basically saying
if exampleVarriable is not equal to nil then print("something...")
~= means not equal. For example, 1 ~= 2
If both numbers(or strings, or tables, etc.) are not equal to each other, it will return true. If both are equal, it will return false.
~=
is Lua's inequality comparison operator.
a ~= b
will evaluate to true
if a
and b
are not equal. However if either a
or b
have a metatable with an __eq
field set to a function then Lua will call that function instead. To avoid calling this function and perform a raw comparison, Lua provides a function call rawequal
which compares a
and b
without invoking the __eq
metamethod.
This last bit of information is really only of use to you if you make use of metatables, but I found it interesting to bring up anyway.
An interesting thing to note about equality comparisons is that {} == {}
will never be true
, as tables are compared by reference and not by value. This means that a variable holding a table will only ever be equal to another if the second refers to the same table in memory:
local t1 = {} local t2 = {} print(t1 == t2) -- false t3 = t1 print(t1 == t3) -- true
Yet another thing to note is that NaN
-- a special number constant meaning "Not a Number" and returned by undefined math operations -- equals nothing, not even itself.
local x = 0/0 print(x ~= x) -- true