簡體   English   中英

如何通過for循環從C / C ++函數將表的表返回給Lua

[英]How to return a table of tables to Lua from C/C++ function, through a for loop

我有一個std :: list對象,並且我想給Lua一個返回其2D位置的函數。 所以我需要創建一個表表

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

由於所有內容都在列表中,因此我需要在迭代列表時創建它。

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

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

        // what do I do here

        ++tableIndex;
    }

    // returns the table
    return 1;

由整數鍵以及“ x”和“ y”索引:

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

我會通過反復試驗嘗試嘗試,但是由於我目前不知道/沒有調試方法,所以我真的迷路了。

它會像這樣:

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);  // a 2 elements subtable
    lua_pushnumber(L, it->x);
    lua_rawseti(L, -2, 1);     // x is element 1 of subtable
    lua_pushnumber(L, it->y);
    lua_rawseti(L, -2, 2);     // y is element 2 of subtable
    lua_rawseti(L, -3, tableIndex++)    // table {x,y} is element tableIndex
}
return 1;

警告:這是我腦中未經測試的代碼...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM