lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Ok, i think i got it..

Being new to lua, i don't quite know my way around that well, and i ended up shooting myself in the foot. Repeatedly.

The way i'm going to do it is, having only 1 table:

Vec2 = {
   __index = function(table, key)
       if getmetatable(table)[key] then
           return getmetatable(table)[key]
       end
      -- handle my C++ properties nicely
   end;
   new = function (self, x,y)
       return setmetatable({}, Vec2 );
   end;
   mag = function (self)
       return math.sqrt(self.x * self.x + self.y * self.y)
   end;
}

That way, when lua goes to execute this:

v = Vec2:new()
print (v:mag())

It will see that 'v' doesn't have 'mag', will go to its metatable's metamethod '__index', which will look it up it v's metatable (Vec2), and then return it, and since it's a function, will execute it with whatever params it has. If it doesn't have 'mag', then it will assume that it's a property and handle it.

I suppose this is just a simple dispatcher. It works nicely in lua, and i thing think i'll have any problems in the C++ side.

Is this the "correct" approach?
Any better way of doing it?


David Morris-Oliveros wrote:

First of all, sorry for the very very long post. If things like these are considered bad, please let me know. Thanks

[...snip...]

--
// David Morris-Oliveros
// Camera Coder
// Team Bondi


------------------------------------------------------------------------
Contact:
Team Bondi Pty Ltd
Level 2, 608 Harris Street
Ultimo, NSW 2007
Australia
Tel: +61 (0)2 8218 1500
Fax: +61 (0)2 8218 1507
Web: http://www.teambondi.com
------------------------------------------------------------------------
This email may contain confidential information.  If you are not
the intended recipient, you may not copy or deliver this message to
anyone. In such case, you should destroy this message and kindly
notify the sender by reply email. Opinions, conclusions and other
information in this message that do not relate to the official business
of our firm shall be understood as neither given nor endorsed by it.
------------------------------------------------------------------------