[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Nested userdata
- From: Jonathan <jonl86_05@...>
- Date: Mon, 16 Feb 2009 10:10:06 -0800 (PST)
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")
// .. ? ..
}