1

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

例子.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 函数中的空白:)

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

4

1 回答 1

2
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
于 2014-11-02T14:17:40.867 回答