lua-users home
lua-l archive

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

Maybe you should reconsider your data structures. Do you really want to do the "nesting" on the C++ side, or could you do it better in Lua?

I would see such problems arising when adding scriptability to existing C++ API's. There one cannot freely change the balance as one would when designing a system from scratch. If this is not the case, please give reasons as to why you must embed A within B (I presume there's more fields, too, otherwise they would be essentially equal?).


Jonathan kirjoitti 16.2.2009 kello 20:10:

I would like to access the following C++ types from lua:

struct A { int x, y; };
struct B { A a; }

and then in lua do:

b = B:new()
var = b.a.x

So the best way I can explain this is that I want to have "nested" userdata. Here's what I have so far:

static int B_new (lua_State *L)
 B *b = (B*)lua_newuserdata(L, sizeof(B));
 luaL_getmetatable(L, "B");
 lua_setmetatable(L, -2);
 return 1;

static int A_index (lua_State *L)
   // push 'x' or 'y' depending on key

static int B_index (lua_State *L)
// what do I push if the key is 'a'? I don't want to allocate new userdata, but I also don't want light user data because I need the metatable.

   if (lua_tostring(L, 2) == "a")
      // .. ? ..