C++ Lua(对象的表的表)

我想在Lua中将对象向量的向量转换为对象的表格,所以我有一个简单的Lua脚本:

objects = getObjects()
explosion = objects[1][1]:getDestructible()
print (explosion)

这是我的.cpp文件:

std::vector<std::vector<Area> > objects;
int x;
int y;

y = 0;
lua_newtable(L);
for (std::vector<std::vector<Area> >::iterator it = objects.begin(); it != objects.end(); ++it)
{
    lua_createtable(L, 2, 0);
    x = 0;
    for (std::vector<Area>::iterator it2 = it->begin(); it2 != it->end(); ++it2)
    {
        lua_newtable(L);
        luaL_getmetatable(L, "luaL_Object");
        lua_setmetatable(L, -2);
        lua_rawseti(L, -2, x + 1);
        x++;
    }
    lua_rawseti(L, -2, y);
    y++;
}

当我运行脚本时,总是得到类似“尝试索引空索引”的东西。我错过了什么吗?

点赞
用户4555842
用户4555842

感谢大家的帮助!我终于找到了我的问题的答案,这就是:

int Lua::luaGetObjects(lua_State *L)
{
    int x;
    int y;

    y = 0;
    lua_newtable(L);
    for (std::vector<std::vector<Area> >::iterator it2 = objects.begin(); it2 != objects.end(); ++it2)
    {
        x = 0;
        lua_newtable(L);
        for (std::vector<Area>::iterator it = it2->begin(); it != it2->end(); it++, x++)
        {

            lua_pushnumber(L, x);

            luaL_getmetatable(L, "luaL_Object");
            lua_setmetatable(L, -2);

            lua_rawseti(L, -2, x + 1);
            x++;
        }
        lua_rawseti(L, -2, y + 1);
        y++;
    }
    return 1;
}

然后我可以调用:

objects = getObjects()
explosion = objects[1][1]:getExplosion()
print(explosion)
2015-05-26 21:03:26