lua-users home
lua-l archive

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


Hello,

In a previous thread called "Direct accessing of application variables from
lua" (http://www.egroups.com/message/lua-l/1826?&start=1806)  we discussed
using lua's elegant userdata implemenation to pass pointers of my game's
variables into the scripting environment.  During the discussion, lhf said
the following:

> A cleaner solution would be to create a single global variable in Lua,
> called "C" for clarity, and then talk with your C host via the "C"
variable,
> which woud contain a table and have appropriate get/settable tag methods.
> So, in your Lua code, you could write
>	C.twkHeightFactor=1.2
>or
>	print(C.twkHeightFactor)
>
>(perhaps "C" should be called "twk" instead, in this case).
This brings up my next challenge - how do I pass pointers to structures and
classes from my game to lua and back?  I'm assuming I use I use
get/settable, however it's not clear to me how I keep track of the pointers
using these tag methods.

For example, if my code defines the following:

in c++:

typedef struct Colour
{
	float r, g, b, a;  // I could also have methods in here to
set/get/manipulate the data
};

Colour myColour;		// global within my application and lua
...


in lua:

myColour = {210, 0, 0, 0}
myColour.a = 20
...


How do I save the pointer to myColour?  Do I use userdata and
get/setuserdata as was described previously?  Or do I set a tag method for
get/settable for myColour?  If so, how do I access the elements within the
Colour type, since they aren't indexed (and the docs describe using
get/settable with indexes)?

lhf's response to my previous email had a small code snippet that very
clearly described how this is done (thank you, lhf).  Could something
similar for the above be done as well?

Any help would be much appreciated.
Thank you!
Falko