0

我正在使用 Visual C++ 2012 并尝试为 Lua 编写 ac 扩展。目前我正在设计一个功能原型:

lib.myfunc(number, {a=1,b=2,c=3},{d=4,e=5,...})

'myfunc'函数有3个参数,第一个参数是一个整数,第二个和第三个参数是表格类型,我需要通过键访问值(键是'a','b',' C'...)

我已经阅读了 lua 手册并搜索了许多教程,但我仍然无法让它工作。我想要一个示例 C 代码来完成这项工作,谢谢~</p>

4

1 回答 1

1

我真的不知道 luabind,所以我不确定他们是否提供任何自己的设施来做到这一点,但在 Lua 中你会这样做:

int myLuaFunc(lua_State *L)
{
  int arg1 = luaL_toint(L, 1);
  luaL_checktype(L, 2, LUA_TTABLE);    //Throws an error, if it's not a table
  luaL_checktype(L, 3, LUA_TTABLE);

  //Get values for the first table and push it on the stack
  lua_getfield(L, 2, "keyname");   //Or use lua_gettable
  //Assuming it's a string, get it
  const char *tmpstr = lua_tostring(L, -1);

  //..... Similariliy for all the other keys
}

你可能想参考Lua 参考手册来了解我使用的函数的描述。

于 2013-04-21T10:34:47.790 回答