【问题标题】:Accessing Lua global tables with C++使用 C++ 访问 Lua 全局表
【发布时间】:2017-01-08 22:18:03
【问题描述】:

如何使用 C++ 访问 Lua 中已经存在的全局表? 下面是我尝试过的代码。我尝试创建一个全局变量并尝试将本地变量修改为 Lua 中的本地变量,但事情似乎不起作用

        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);
        }

卢阿

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

更新 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;
}

卢阿

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();

【问题讨论】:

  • 要访问全局变量,请执行lua_getglobal(L, "problem");。但是您的代码有更多问题。您的表是一个数组数组,您的代码尝试迭代由键和值组成的简单表。查看其他问题以供参考stackoverflow.com/questions/29287988/…

标签: c++ lua global-variables


【解决方案1】:

您没有任何值可在 Lua 堆栈上进行迭代。
int t=2; 没有反映任何内容,并且您的脚本不会返回要留在堆栈上的值。
有关访问全局表的示例,请参阅 PIL book: 25.1 – Table Manipulation

【讨论】:

  • 变量不是“问题”吗?
  • 是的,但它存储在Lua全局表中,直到你调用lua_getglobal(),或者从带有return语句的脚本返回时它才在Lua栈中。
  • 您将如何在 C++ 中迭代或拥有该表的副本?
  • 你已经有了迭代部分。您只是没有表可以在 Lua 堆栈上进行迭代。在运行循环之前使用 lua_getglobal() 获取表。
  • 你能在更新 1 的帖子中帮我检查一下吗?我更新了代码。但是存在一些问题,它说有事要做的代码将为零并且程序崩溃
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
  • 2012-02-26
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 2011-01-29
相关资源
最近更新 更多