1

I have the following nested table defined in Lua. I need to pass it into my C++ program so that I can accurately read it's values. I know I can pass a single table to Lua using the

lua_getglobal(L, "parameters") 

function. But in this case since it is nested, how can I deal with it?

parameters = 
{ 
     fuel = 100,         
     damage = 250,
     maxspeed = 250,
     levels = 
     {
         { 
             fuel = 200,
             damage = 600, 
             maxspeed = 300,                 
         },
         { 
             fuel = 300,
             damage = 750,
             maxspeed = 400, 
         },                    
     }
}
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
shaveenk
  • 1,983
  • 7
  • 25
  • 37

1 Answers1

1

The lua_getglobal will work for "parameters" just like any other variable. You will receive the table on the top of the stack. Once there, you can use the lua_gettable(), lua_getfield() and lua_rawget*() functions to access its fields. The fact that the table is nested makes no difference in accessing it at all. To access a subtable, push it onto the stack with those functions and access it in the same way as its "parent" table.

user1095108
  • 14,119
  • 9
  • 58
  • 116