lua-users home
lua-l archive

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


This approach works fine, but there is one piece missing: if the vectors are
lua tables, you can actually enumerate the fields in lua (with foreach(),
for example). So you can print a vector with foreach(v,print). Since there
are currently no tag methods for enumerating elements, you can't really make
the behavior of usertags indistinguishable from that of true lua tables for
now.

Bruno Costa Silva
RPG, Adventure and Technology Games Group
Microsoft Corp.

-----Original Message-----
From: Diego Nehab [mailto:diego@tecgraf.puc-rio.br]
Sent: Wednesday, June 07, 2000 6:24 PM
To: Multiple recipients of list
Subject: Re: Conceptual problem with gettable tag method


Hi Falko,

I once had a similar decision problem while implementing an image data
type in Lua. I believe the simpler solution to your problem is to stick
to usertag carrying pointers to your C structure. That is, don't push
lua tables from C to Lua. Instead, stick to C pointers to your C
structures, passed to lua as usertag objects. You should define the
gettable and settable methods for the new vector tag. Your getglobal
method would push a usertag object into the c2lua stack, carrying the
pointer to the appropriate C structure.

Here are the new actions for the function calls:

localvec = newVector(10, 10, 10)
-- calls lua_pushusertag to push a pointer to a C structure containing
the 
-- vector (10, 10, 10), as an usertag object with the vector tag, into
the c2lua stack

localvec = GlobalVec
-- calls lua_pushusertag to push the pointer to the C structure
corresponding to the C 
-- global variable "GlobalVec", as an usertag object with the vector
tag, into the c2lua stack

localnum = GlobalVec.x
-- lua will call getglobal for "GlobalVec" and your method will do the
same as it did in the
-- line above. Then, lua will call the gettable tagmethod for the vector
tag, passing
-- your usertag vector object. your tagmethod should then push the
number value of
-- "x" into the c2lua stack

GlobalVec.x = localnum
-- lua will call getglobal for "GlobalVec" and your method will do the
same as it did in the
-- previous examples. Then, lua will call the settable tagmethod for the
vector tag, passing
-- your usertag vector object and the value of localnum. your tagmethod
should then update
-- the field corresponding to "x" in the C structure

You might want to implement the gc tag method for your vectors, to avoid
memory leaks.

Hope it helps,
regards,
Diego.