[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Comparing userdata with other types
- From: Ivo Beltchev <ivo@...>
- Date: Sun, 31 May 2009 11:41:47 -0700
Hi
I am embedding Lua in my application and I want to expose some objects
to the Lua scripts. Different objects have different sets of properties.
For example object1 can have speed and length and object2 can have mass
and friction.
Currently if the script wants to check if an object has mass>20 it has
to do:
if (obj.mass and obj.mass>20)...
because obj.mass may be nil and then comparing it to 20 fails. So for
properties that don't exist I want to create a special object (NIL) that
can be safely used in expressions:
NIL+x -> NIL
NIL..x -> NIL
NIL[x] -> NIL
#NIL -> 0
NIL==NIL -> true
NIL==nil -> true (not so sure about this)
NIL>20 -> false
...
You get the idea.
Then I can simply write:
if (obj.mass>20) ...
However the __lt metamethod is not being called when I compare NIL to a
number. It is used only if the two values are of the same type.
Otherwise Lua throws an error.
Any ideas how to do this? A NAN value almost does what I want but it
can't be indexed, and also NAN~=NAN.
Thanks
Ivo