<small id='0rK05'></small><noframes id='0rK05'>

  1. <tfoot id='0rK05'></tfoot>

  2. <legend id='0rK05'><style id='0rK05'><dir id='0rK05'><q id='0rK05'></q></dir></style></legend>

      <i id='0rK05'><tr id='0rK05'><dt id='0rK05'><q id='0rK05'><span id='0rK05'><b id='0rK05'><form id='0rK05'><ins id='0rK05'></ins><ul id='0rK05'></ul><sub id='0rK05'></sub></form><legend id='0rK05'></legend><bdo id='0rK05'><pre id='0rK05'><center id='0rK05'></center></pre></bdo></b><th id='0rK05'></th></span></q></dt></tr></i><div id='0rK05'><tfoot id='0rK05'></tfoot><dl id='0rK05'><fieldset id='0rK05'></fieldset></dl></div>
        <bdo id='0rK05'></bdo><ul id='0rK05'></ul>
    1. 在 C++ 中循环遍历所有 Lua 全局变量

      时间:2023-09-27
        <tbody id='pceOF'></tbody>

      <small id='pceOF'></small><noframes id='pceOF'>

      • <legend id='pceOF'><style id='pceOF'><dir id='pceOF'><q id='pceOF'></q></dir></style></legend>

        • <i id='pceOF'><tr id='pceOF'><dt id='pceOF'><q id='pceOF'><span id='pceOF'><b id='pceOF'><form id='pceOF'><ins id='pceOF'></ins><ul id='pceOF'></ul><sub id='pceOF'></sub></form><legend id='pceOF'></legend><bdo id='pceOF'><pre id='pceOF'><center id='pceOF'></center></pre></bdo></b><th id='pceOF'></th></span></q></dt></tr></i><div id='pceOF'><tfoot id='pceOF'></tfoot><dl id='pceOF'><fieldset id='pceOF'></fieldset></dl></div>

              <bdo id='pceOF'></bdo><ul id='pceOF'></ul>
              <tfoot id='pceOF'></tfoot>
                本文介绍了在 C++ 中循环遍历所有 Lua 全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我已经搜索了很长时间,但我还没有找到从 C++ 中获取所有全局变量的方法.考虑这个小的 Lua 测试脚本.

                I have been searching for quite a while now and I haven't found a way to fetch all the global variables from C++. Consider this small Lua test script.

                myGlobal1 = "Global 1"
                myGlobal2 = 2
                
                function test()
                  local l1=0
                  print (myGlobal1,myGlobal2,l1)
                end
                
                test()
                

                假设您在 print (myGlobal1,myGlobal2,l1) 处暂停执行并从 C++ 获取所有全局变量(myGlobal1myGlobal2).这些例子是任意的,全局变量,从 C++ 的角度来看,是未知的.

                Assume you pause the execution at print (myGlobal1,myGlobal2,l1) and from C++ get all the global variables (myGlobal1 and myGlobal2). These examples are arbitrary, the global variables, from a C++ point of view, are unknown.

                我一直在查看 lua_getglobal() 但后来我需要先知道变量的名称.我查看了 lua_getupvalue() 但只得到了_ENV"作为结果.

                I have been looking at lua_getglobal() but then I need to know the name of the variable first. I looked at lua_getupvalue() but only got "_ENV" as result.

                我想我可以在知道它们的名称后立即使用 lua_getglobal(),但是如何获取全局变量列表(来自 C++)?我现在确实有 lua_Debug 结构(如果有帮助的话)

                I guess I can use lua_getglobal() as soon I know the name of them, but how do I get the list of global variables (from C++)? I do have the lua_Debug structure at this point (if it is to any help)

                编辑这篇文章最初不是关于遍历表,而是关于如何找到用户自己的全局变量.

                EDIT This post wasn't originally about iterating through a table, it was about how to find the user's own globals.

                但是,我发布了一个解决方案来说明如何做到这一点这里.

                However, I posted a solution to how this can be done here.

                推荐答案

                好的,我解决了.

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

                lua_next() 找不到更多条目时,键名会弹出,将表格留在顶部 (-1).

                When lua_next() can't find more entries the key name is popped leaving the table on top(-1).

                下一个问题是将我自己的全局变量与其余的表条目区分开来...

                Next problem would be to distinguish my own globals from the rest of the table entries...

                这篇关于在 C++ 中循环遍历所有 Lua 全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:“未定义的引用"使用 Lua 下一篇:在 Lua 中注册 C++ 函数?

                相关文章

                    <bdo id='CMxDK'></bdo><ul id='CMxDK'></ul>
                1. <i id='CMxDK'><tr id='CMxDK'><dt id='CMxDK'><q id='CMxDK'><span id='CMxDK'><b id='CMxDK'><form id='CMxDK'><ins id='CMxDK'></ins><ul id='CMxDK'></ul><sub id='CMxDK'></sub></form><legend id='CMxDK'></legend><bdo id='CMxDK'><pre id='CMxDK'><center id='CMxDK'></center></pre></bdo></b><th id='CMxDK'></th></span></q></dt></tr></i><div id='CMxDK'><tfoot id='CMxDK'></tfoot><dl id='CMxDK'><fieldset id='CMxDK'></fieldset></dl></div>
                2. <tfoot id='CMxDK'></tfoot>
                  1. <legend id='CMxDK'><style id='CMxDK'><dir id='CMxDK'><q id='CMxDK'></q></dir></style></legend>

                    <small id='CMxDK'></small><noframes id='CMxDK'>