如何通过for循环从C/C++函数将一张表的表格返回给Lua

我有一个对象的std::list,并且我想给Lua一个返回其2D位置的函数。因此,我需要创建一个表格,其中包含表格:

{ {x,y}, {x,y}, {x,y}...}

并且由于所有内容都在列表中,因此我需要在迭代列表时创建它。

lua_newtable(L_p);  //在0处的表
int tableIndex = 1; //第一条目录为1

for(std :: list <AmmoDropped *>::iterator it = m_inputAmmosDropped.begin();
        it!= m_inputAmmosDropped.end();
        ++ it){

        //我在这里该做什么

        ++ tableIndex;
}

//返回表
返回1;

按整数键和“x”和“y”进行索引:

 positions [0] .x
 positions [0] .y

我会通过尝试错误来尝试,但由于我现在不知道/没有调试它,所以我真的很迷失。

点赞
用户1183484
用户1183484
它将像这样进行:

lua_newtable(L); // table at 0 int tableIndex = 1; // first entry at 1

for(std::list<AmmoDropped*>::iterator it = m_inputAmmosDropped.begin(); it != m_inputAmmosDropped.end(); ++it ){ lua_createtable(L, 2, 0); //创建一个 2 个元素的子表格 lua_pushnumber(L, it->x); lua_rawseti(L, -2, 1); // x 是子表格的第一个元素 lua_pushnumber(L, it->y); lua_rawseti(L, -2, 2); // y 是子表格的第二个元素 lua_rawseti(L, -3, tableIndex++) // 将 {x,y} 表格作为第 tableIndex 个元素 } return 1;

```

警告:这是我从头上脑海中未经测试的代码...

2012-11-11 00:15:02