lua-users home
lua-l archive

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

Carsten Fuchs wrote:

In order words, my question rephrased:
How can I have a complex C++ datastructure, and work with it *both* in C++ *and* in Lua code??


I'm not sure this answers your question in its entirety, but certainly should provide you with some ideas.

My approach to exposing a C++ object to Lua was to create a table with functions on it that mirror those on the C++ object being exposed.

So, for the example of "WindowT" you may want to create a "LuaWindow" table with keys such as setX, setY and whatever else you would like to have exposed to Lua. You would want to have a separate table for each instance of WindowT. In order to have this table know about your C++ object, you can use lightuserdata. For example, assuming that a LuaWindow is on the top of the stack and you wish to 'new' it's C++ counterpart:

   WindowT* your_window = new WindowT;


Now, when you call setX or setY on that table, the first thing the function would do is get the table on the bottom of the stack and cast it's "object" key back to a WindowT. Then, supposing that setX took a float as a parameter, you would cast the /second/ element on the stack from the bottom to a float and forward that result to your C++ object.

   lua_pushstring(L, "object");
   lua_gettable(L, 1);
   WindowT* window = static_cast<WindowT*>(lua_touserdata(L, -1));
   lua_pop(L, 1);

   window->setX(lua_tonumber(L, 2));
   return 0;

As for writing scripts that get triggered by events, perhaps you could pass the appropriate LuaWindow to the script? What is unique about your problem though, is the hierarchy of WindowT's. This shouldn't cause to much of a problem. I've haven't addressed this myself, as all of my objects are constructed in Lua. That way, it is perfectly possible for me to do this:

   main = LuaWindow:new()
   child = LuaWindow:new()


Not sure if you want to do it this way however.

Matt Comi.

Big Bucket Amusement
This email is confidential and intended solely for the use of the individual to whom it is addressed. Any views or opinions presented are solely those of the author and do not necessarily represent those of NAUTRONIX LTD.

If you are not the intended recipient, you have received this email in error and use, dissemination, forwarding, printing, or copying of this email is strictly prohibited. If you have received this email in error please contact the sender.
Although our computer systems use active virus protection software, and we take various measures to reduce the risk of viruses being transmitted in e-mail messages and attachments sent from this company, we cannot guarantee that such e-mail messages and attachments are free from viruses on receipt.  It is a condition of our using e-mail to correspond with you, that any and all liability on our part arising directly or indirectly out of any virus is excluded.  Please ensure that you run virus checking software on all e-mail messages and attachments before reading them.