在C++函数中创建一个Lua表格

我写了一个C++函数,用于获取容器中的“items”。我需要把这些变量放到表格中,但无论我做什么,脚本总是覆盖表格的第一个单元格。

我使用Lua 5.0

Container *box = dynamic_cast<Container*>(item);
if(box)
{

lua_newtable(L);
int top = lua_gettop(L);
int n = box->lcontained.size();

for(int i = 0; i <= n; i++)
{

Item* karta = box->getItem(i);
if(karta)
{

setField(L,"slot", i);
setField(L,"kartaid", karta->getID());

lua_settop(L, top);

}

}

}

原文链接 https://stackoverflow.com/questions/8255341

点赞
stackoverflow用户734069
stackoverflow用户734069
`setField(L,"slot", i);`

注意:Lua 使用 _从一开始_ 的索引。因此,在与 Lua 交互的 C++ 代码中,您也必须使用从一开始的索引,即需要 `i+1`。
2011-11-24 19:57:27