【问题标题】:Lua: Read table parameter from c function callLua:从c函数调用中读取表参数
【发布时间】:2014-11-02 22:17:40
【问题描述】:

我真的不确定在 Lua 的 C API 中处理表。 我目前正在开发的界面需要我阅读 给我的 c 函数的表的内容:

example.lua:

myVector2 = {["x"]=20, ["y"]=30}
setSomePosition(myVector2)

我注册为“setSomePosition”的C函数:

static int lSetSomePosition(lua_State *L)
{
    //number of arguments
    if(lua_gettop(L) != 1)
    {
        //error handling
        return 0;
    }
    //Need your help with the following:
    //extract tables values of indexes "x" and "y"

    return 0;
}

我知道有几种处理表格的方法,有时您需要知道我所做的索引。我现在对此感到困惑,我研究得越多,我就越困惑。可能是因为我真的不知道如何用适当的术语来描述我所追求的。

非常感谢一些很好的注释示例代码,说明您将如何填补我的 c 函数中的空白:)

(如果您对此主题有易于理解的指南,请不要介意评论)

【问题讨论】:

标签: lua lua-table lua-api


【解决方案1】:
lua_getfield(L, 1, "x") //pushes a value of t["x"] onto the stack
lua_tonumber(L, -1) //returns the value at the top of the stack
lua_getfield(L, 1, "y") //pushes a value of t["y"] onto the stack
lua_tonumber(L, -1) //returns the value at the top of the stack

【讨论】:

  • lua_getfield 调用中的正数代表什么?就像当 c 函数调用接收到 2 个表时,我可以将其递增 1 以将值从第二个表推送到堆栈?
  • @InDieTasten 这是第一个参数(即表格)的堆栈索引。
猜你喜欢
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
相关资源
最近更新 更多