1

I have a std::list of objects, and I want to give Lua a function that returns its 2D positions. So I need to create a table of tables

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

And since its all on a list, I need to create it while iterating the list..

    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;

Indexed by integer keys, and by 'x' and 'y':

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

Id try by trial and error, but since I don't know / don't have how debug it for now, I'm really lost.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Icebone1000
  • 1,231
  • 4
  • 13
  • 25

1 Answers1

1

It will go like this:

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;

warning: this is untested code from the top of my head...

Gilles Gregoire
  • 1,696
  • 1
  • 12
  • 14