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.

2

1 Answer 1

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.