3

How can I access a global table that already exists in Lua using C++ ? Below are code which I tried. I tried creating a global variable and try modifying a that local to the local in Lua but things dont seem to work

        lua_State *lua_state = luaL_newstate();
        luaL_openlibs(lua_state);
        // lua_createtable(lua_state, 0, 81);
        // for (int i = 1; i <= 81; i++)
        // {
        //  lua_pushnumber(lua_state, i);
        //  lua_pushnumber(lua_state, grid_[i - 1]);
        //  lua_settable(lua_state, -3);
        // }
        // 
        // lua_setglobal(lua_state, "arg");

        // lua_createtable(lua_state, 81, 1);
        // 
        // for (int i = 1; i <= 81; i++)
        // {
        //  lua_pushnumber(lua_state, i);
        //  lua_pushnumber(lua_state, grid_[i - 1]);
        //  lua_settable(lua_state, -3);
        // }
        // lua_setglobal(lua_state, "arg");


        luaL_loadfile(lua_state, "main.lua");
        lua_call(lua_state, 0, 0);

        int t = 2;

        /* table is in the stack at index 't' */
        lua_pushnil(lua_state);  /* first key */
        while (lua_next(lua_state, t) != 0) {
            /* uses 'key' (at index -2) and 'value' (at index -1) */
            printf("%s - %s\n",
                lua_typename(lua_state, lua_type(lua_state, -2)),
                lua_typename(lua_state, lua_type(lua_state, -1)));
            /* removes 'value'; keeps 'key' for next iteration */
            lua_pop(lua_state, 1);
        }

Lua

problem =
{
    {9, 0, 0, 1, 0, 0, 0, 0, 5},
    {0, 0, 5, 0, 9, 0, 2, 0, 1},
    {8, 0, 0, 0, 4, 0, 0, 0, 0},
    {0, 0, 0, 0, 8, 0, 0, 0, 0},
    {0, 0, 0, 7, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 2, 6, 0, 0, 9},
    {2, 0, 0, 3, 0, 0, 0, 0, 6},
    {0, 0, 0, 2, 0, 0, 9, 0, 0},
    {0, 0, 1, 9, 0, 4, 5, 7, 0},
}

Update 1

int main()
{
    lua_State *lua_state = luaL_newstate();
    luaL_openlibs(lua_state);
    luaL_loadfile(lua_state, "main.lua");

    lua_getglobal(lua_state, "problem");

    //lua_pushglobaltable(lua_state);       // Get global table
    lua_pushnil(lua_state);               // put a nil key on stack
    while (lua_next(lua_state, -2) != 0) { // key(-1) is replaced by the next key(-1) in table(-2)
        std::string name = lua_tostring(lua_state, -2);  // Get key(-2) name
        std::cout << name << std::endl;
        lua_pop(lua_state, 1);               // remove value(-1), now key on top at(-1)
    }
    lua_pop(lua_state, 1);                 // remove global table(-1)

    lua_call(lua_state, 0, 0);

    return 0;
}

Lua

problem =
{
    {9, 0, 0, 1, 0, 0, 0, 0, 5},
    {0, 0, 5, 0, 9, 0, 2, 0, 1},
    {8, 0, 0, 0, 4, 0, 0, 0, 0},
    {0, 0, 0, 0, 8, 0, 0, 0, 0},
    {0, 0, 0, 7, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 2, 6, 0, 0, 9},
    {2, 0, 0, 3, 0, 0, 0, 0, 6},
    {0, 0, 0, 2, 0, 0, 9, 0, 0},
    {0, 0, 1, 9, 0, 4, 5, 7, 0},
}

print("Lua Works")
user_input = io.read();
JekasG
  • 161
  • 1
  • 5
  • 14
  • To access a global variable, do `lua_getglobal(L, "problem");`. But your code has more issues. Your table is an array of arrays, and your code tries to iterate a simple table compose of keys and values. Check out this other question for reference http://stackoverflow.com/questions/29287988/iterating-over-table-of-tables-with-the-lua-c-api – Diego Pino Sep 01 '16 at 08:18

1 Answers1

0

You don't have any values to iterate on Lua stack.
That int t=2; doesn't reflect anything, and your script doesn't return values to be left on stack.
See PIL book: 25.1 – Table Manipulation for examples on accessing global table.

Vlad
  • 5,450
  • 1
  • 12
  • 19
  • Isn't 'problem' the variable ? – JekasG Sep 01 '16 at 08:17
  • It is, but it is stored in Lua global table, and it's not on Lua stack until you call lua_getglobal(), or return from script with `return` statement. – Vlad Sep 01 '16 at 08:19
  • How would you iterate or have a copy of that table in C++ ? – JekasG Sep 01 '16 at 08:22
  • You already have iteration part. You just don't have table to iterate on Lua stack. Get table there with lua_getglobal() before running loop. – Vlad Sep 01 '16 at 08:40
  • Could you check for me in Update 1 , in the post . I updated the code . But there are problems which the code where it say there is something to do will nil and the program crashes – JekasG Sep 01 '16 at 09:03
  • You removed call to lua_call(). Script didn't run this time, so 'problem' variable wasn't created in Lua vm, and lua_getglobal() returns nil. – Vlad Sep 01 '16 at 09:38
  • When i place lua_call() before lua_getglobal() . When the while loops get run the second time . There is a " Fatal program exit request " – JekasG Sep 01 '16 at 12:05
  • Did you leave lua_call() after the loop? Just like it is in "update 1"? – Vlad Sep 01 '16 at 15:29