1. 示例
lua代码传参
local tb = {1,2,3,4,5}
testfunLua(self, tb, #tb)
C++接收
if (!tolua_istable(L, 2, 0, &tolua_err))
{
tolua_error(L, "invalid arguments in function 'lua_testfunLua'", nullptr);
return 0;
}
int size = tolua_tonumber(L, 3, 0);
vector<int> vcNum;
for (int i = 0; i < size; i++)
{
lua_pushnumber(L, i + 1);
lua_gettable(L, 2);
int iNum = tolua_tonumber(L, -1, 0);
vcNum.push_back(iNum);
lua_pop(L, 1);
}
self->testFun(vcNum);
lua_settop(L, 1);
return 1;
2. 关于接口说明
2.1 根据已知key获取table元素:
lua_gettable(Lua_state,index) 用于取出index处的table中栈顶作整形key值对应的value lua_getfield(Lua_state,index,key) 用来直接获取index处的table栈顶字符串key对应的value
下面这两个用法等价
lua_getfield(stack, -1, "loaded");
lua_pushstring(L,"loaded");
lua_gettable(L,-2);
2.2 根据未知key遍历
lua_pushnill(L);
while(lua_next(L, -2))
{
int iNum = tolua_tonumber(L, -1, 0);
vcNum.push_back(iNum);
lua_pop(L, 1);
}
|