shaveenk
shaveenk

Reputation: 2083

Passing nested tables from Lua to C

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, 
         },                    
     }
}

Upvotes: 1

Views: 471

Answers (1)

user1095108
user1095108

Reputation: 14603

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.

Upvotes: 1

Related Questions