How to use a Lua variable in C ++?

6

I can not get value from an array in Lua to use in C ++.

So I get the value of the variable M :

//No LUA    
M = 85  

//No C++    
L = lua_open();
luaL_loadfile(L, "teste.lua");
lua_pcall(L, 0, 0, 0);

int m;

lua_getglobal(L, "M");
m = lua_tonumber(L, -1);

cout << m;

But I do not know how to get the index value of an array as follows:

// LUA
MATRIZ =
{

   {4,2,2,6},
   {2,1,1,2},
   {2,1,1,2},
   {5,2,2,3}

}

How do I get the value of this array?

    
asked by anonymous 14.08.2014 / 01:52

1 answer

4

You can use the lua_gettable method.

  • You need to add the index to the stack via lua_pushinteger .
  • The key is overlaid with the element.
  • You can also try:

    lua_getglobal(L, "MATRIZ")
    lua_rawgeti(L,-1,1) -- {4,2,2,6}
    lua_rawgeti(L,-1,2) -- 2
    

    Solutions from my question in the StackOverflow (EN).

        
    14.08.2014 / 04:53