lua-users home
lua-l archive

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


I'm having an interesting problem in passing nested tables from C++ to Lua. What I'm looking to do is to construct something like

   a1={x=5,y=8}
   a2={x=6,y=2}
   b={b1=a1,b2=a2}

and then call f(b) and get the returned value back. I don't have any troubles constructing a1 and a2, but constructing b does not seem to work. Here is what I'm trying (assume that a1 and a2 are globals as defined above):


   lua_pushstring(state,"b");

   // Create a new table
   lua_newtable(state);
   // define the key as "b1"
   lua_pushstring(state,"b1");
   // retrieve the table a1
   lua_pushstring(state,"a1");
   lua_gettable(state,LUA_GLOBALSINDEX);
   // set b1 = a1 in the new table
   lua_settable(state,-3);

   // Now do the same for b2=a1
   lua_pushstring(state,"b2");
   lua_pushstring(state,"a2");
   lua_gettable(state,LUA_GLOBALSINDEX);
   lua_settable(state,-3);

   // set the number of keys/indexes in the table to "2"
   lua_pushliteral(state,"n");
   lua_pushnumber(state,2);
   lua_rawset(state,-3);

   // Finally, assign the new table to the global variable "b"
   lua_settable(state,LUA_GLOBALSINDEX);

There must be something I'm missing. I have a similar problem setting one vector equal to another. Say I want to set a1=a2... I would think the following would be appropriate:

   lua_pushstring(state,"a1");
   lua_gettable(state,LUA_GLOBALSINDEX);
   lua_pushstring(state,"a2");
   lua_gettable(state,LUA_GLOBALSINDEX);
   lua_settable(state,LUA_GLOBALSINDEX);

But this does not seem to work either. Suggestions?

=====================================================
Thomas Tongue <TTongue@imagiware.com>
Imagiware Inc. - http://www.imagiware.com/
Internet Presence and Consulting Services
== PGP Public Key: https://ssl1.imagiware.com/ttongue/public.key ==