c++ - 使用 C++ 访问 Lua 全局表

标签 c++ lua global-variables

如何使用 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);
        }

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

更新 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 堆栈上迭代。
int t=2; 没有反射(reflect)任何内容,您的脚本也没有返回要留在堆栈中的值。
参见 PIL book: 25.1 – Table Manipulation有关访问全局表的示例。

关于c++ - 使用 C++ 访问 Lua 全局表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39264603/

相关文章:

c++ - 脚本如何通过在 Lua 中加载不同的脚本来保留其值?

python - 在 python 中,如何使 main args 全局化

Python:未定义全局名称(但它不是全局变量)

c++ - UTF8 字符数组到 std::wstring

C++ 传递结构化内容的方式?

c++ - 是否有忙等待条件或直到超时的标准功能

lua - 在lua源代码中,为什么他们使用double而不是更通用的lua_Number?

Lua:用变量指定文件名

javascript - 全局数组不会在 javascript 函数中被修改

c++ - 为什么这个可变参数计数宏在 VC++ 中失败?