Original Post
I had a hard time finding decent examples of how to traverse a table from a lua script in c++, and got something up and running that hopefully will be more helpful than some of the breif lua documentation for someone starting out. I've written a lua/c++ api wrapper to make writing c++ callhooks for use in lua easier for my other team members not as well versed in lua so the methodology in why im doing things, may or may not be traditional - i havent encountered enough c++ lua source to think there is a traditional way to begin with, and some of the code is out of context, so just bear those facts in mind. however the basic motivation behind this, was a team member wanted to pass a bunch of math structures like an orientation matrix, vectors, positional data, and some other chode from lua to his c code so he asked me to write some functionality that made it easy for him to do so. I'll be using the matrix table as the example since it is an easy single type table, and ill try to be as explainatory as possible. --lua script --basic table, in a matrix style format, with no particlar rhyme or reason --for the values except was easy to debug and know i was getting the elements --in the correct order matrix = { 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16 } --function passing some random values, and then the table testTable(1, 2, 3, "matrix") //C++ code //L is the lua_State*, lua is already set up, and it is assumed //a script calling the function testTable is being executed some where in an //application //remember you need to register your c functions with lua before you call the //script you use them in //ex. lua_register(L, testTable, "testTable"); static int testTable(lua_State *L) { //the table is the 4th parameter passed int size = (int)lua_strlen(L, 4); char *matrixName = new char[size]; matrixName = (char*)lua_tostring(L, 4); //the array to store the table information in float *matrix = new float[16]; //lua_getglobal will grab the table by its name that we just pulled off //the functions stack lua_getglobal(L, matrixName); lua_pushnil(L); //should note that using the string name of the table is just a way to //directly access the table, but you can pass the actual table and then //instead of grabbing the global name and pushing it, you push the value //of the item on the stack - the table like so: // script call : testTable(1, 2, 3, matrix) //if(lua_istable(m_pL, 4)) //4 being the position of the table on the stack // lua_pushvalue(m_pL, 4); // lua_pushnil(L); int index = 0; //lua_next will start the iteration, it needs nil to be the first key it pops //why we called lua_pushnil earlier //the table was pushed onto the stack at -1(-1 is equivalent to lua_gettop) //the lua_pushnil then pushed the table to -2, where it is currently located while (lua_next(L, -2)) // -2 is the table { //grab the meat at the top of the stack //i know what i want is a floating point value, however //you might want to do a lua_isnumber(L, -1) to be sure matrix[index] = (float)lua_tonumber(L, -1); index++; //you extracted that value, pop it off so we can push the next element lua_pop(L, 1); } //you have extracted all of the tables contents //you can pop the table off the stack now lua_pop(L, 1); //do what you want with the table information //push return values back to lua return 0; } its simple and fairly clean imo, heavy comments but hopefully it helped with any confusions, any critiques are gladly welcomed, hope this helps some one out! [Edited by - DCM on June 19, 2006 1:35:13 PM]