because Lua can't use relational operators on objects of different types
there are no types or objects in lua. Comparing two
tables compares there identity, so unless they are exactly the same, how could lua know whether they should be equal?
LastNameGames wrote:Shouldn't calling setmetatable(o,depth) set it so that an object of type o will look for undefined values in depth?
The
metatable isn't where lua looks for missing stuff, it's where lua looks for
metainformation. The
__index key
in the metatable is the table that will be searched for missing keys. We often set the "__index" key of the metatable to be itself so we don't use two tables for one "class":
Code: Select all
local Player = { draw = function () ... end }
Player.__index = Player
local newplayer = setmetatable({x=2, y=3}, Player)
-- same functionality as:
local PlayerDefinition = { draw= function () ... end }
local PlayerMT = { __index = PlayerDefinition }
local newplayer = setmetatable({x=2, y=3}, PlayerMT)
There are many more "special" metatble keys that can alter a table's behaviour, like for example "__lt".
I think this is what you want to do:
Code: Select all
local Depth = {} -- uppercase to indicate this is something you can think of as a "type" - not needed
Depth.__index = Depth
function Depth:__lt( left, right )
return left.depth < right.depth
end
local Player1 = setmetatable({}, Depth) -- because of this dependenceis will "decend" into Depth
Player1.__index = Player1 -- look in Player1 for methods
function Player1:new(x, y)
local o = setmetatable({}, Player1)
end
--analogous for Player2
In your code example the definition of character1/2 is unnecessary, they do not (and can not) serve any relevant purpose.
I am not sure why you would really need a
__lt in the first place though, you can just replace "a < b" with "a.depth < b.depth" in the skiplist code.