从元表字段/方法访问数组索引

Array index access from metatable field/method

本文关键字:访问 数组 索引 方法 字段      更新时间:2023-10-16

我有一些反映一些c++类/结构的元表。我通常依靠__index来调用对象的任何字段/方法,并在一个函数中解析它们。

我遇到的困难是当我想传递参数到一个字段,像这样:

anim = playerInfo.animations
while anim do
  print (anim)
  numstates = anim.numstates
  for i = 1, numstates do
    state = anim.states(i)  <--- This line right here is the issue
    print(state)
  end
  anim = anim.next
end
下面是相关的C代码:
static const struct luaL_Reg objanimationlib_m[] = {
    {"__tostring", objanimation2string},
    {"__index", objanimationget},
    {"__newindex", objanimationset},
    {NULL, NULL}
};
    luaL_newmetatable(L, "objanimation");
    lua_pushvalue(L, -1); // duplicates the metatable
    luaL_setfuncs(L, objanimationlib_m, 0);

__index函数内部:

else if (!strcmp(field, "states"))
{
    int number = (int)luaL_checknumber(L, 3) - 1; // -1 cuz Lua is not 0-based
    if (number >= anim->numstates)
        return 0;
    PushUserdata(&anim->states[number], "objstate");
}

运行脚本,我得到一个错误:

Warning: [string "test.lua"]:13: bad argument #3 to '__index' (number expected, got no value)
我觉得我错过了一些愚蠢的简单的东西。这是什么?

编辑:这是我的解决方案,内部__index函数:

else if (!strcmp(field, "states"))
{
    lua_newtable(L);
    int i;
    for (i = 0; i < anim->numstates; i++)
    {
          PushUserdata(&anim->states[i], "objstate");
          lua_rawseti(L, -2, i+1);
    }
}

返回一个充满userdata元素的表。可能会很昂贵,所以这也会提高性能:

anim = playerInfo.animations
while anim do
  print (anim)
  numstates = anim.numstates
  states = anim.states
  for i = 1, numstates do
    print(states[i])
  end
  anim = anim.next
end

state = anim.states(i)

等价于

do local f=anim.states; state=f(i) end

所以你的元方法永远不会看到i

换句话说,index元方法接收两个参数,表和键。它返回的内容不一定服从于任何元方法,除非您显式地这样做。

我去定义__len返回numstates__call处理anim.states(i),这样你的代码可以写

  for i = 1, #anim do
    state = anim(i)
    print(state)
  end