【问题标题】:Passing nested tables from Lua to C将嵌套表从 Lua 传递到 C
【发布时间】:2014-08-28 19:49:00
【问题描述】:

我在 Lua 中定义了以下嵌套表。我需要将它传递到我的 C++ 程序中,以便我可以准确地读取它的值。我知道我可以使用

将单个表传递给 Lua
lua_getglobal(L, "parameters") 

功能。但是这种情况下既然是嵌套的,那我该如何处理呢?

parameters = 
{ 
     fuel = 100,         
     damage = 250,
     maxspeed = 250,
     levels = 
     {
         { 
             fuel = 200,
             damage = 600, 
             maxspeed = 300,                 
         },
         { 
             fuel = 300,
             damage = 750,
             maxspeed = 400, 
         },                    
     }
}

【问题讨论】:

    标签: c++ lua lua-table nested-table lua-api


    【解决方案1】:

    lua_getglobal 将适用于“参数”,就像任何其他变量一样。您将收到位于堆栈顶部的表格。在那里,您可以使用lua_gettable()lua_getfield()lua_rawget*() 函数访问其字段。表是嵌套的这一事实在访问它时根本没有区别。要访问子表,请使用这些函数将其压入堆栈,并以与其“父”表相同的方式访问它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-27
      • 2011-01-14
      • 1970-01-01
      • 2013-07-24
      • 2010-10-24
      • 1970-01-01
      • 2017-07-04
      • 2015-01-18
      相关资源
      最近更新 更多