Gam*_*eak 5 c++ api lua lua-table
如何将Lua中未知长度的表传递给绑定的C++函数?
我希望能够像这样调用Lua函数:
call_C_Func({1,1,2,3,5,8,13,21})
Run Code Online (Sandbox Code Playgroud)
并将表格内容复制到一个数组(最好是STL矢量)?
如果您使用LuaBind,那么就像一次注册调用一样简单。至于你自己的,你需要看一下lua_next函数。
基本上代码如下:
lua_pushnil(state); // first key
index = lua_gettop(state);
while ( lua_next(state,index) ) { // traverse keys
something = lua_tosomething(state,-1); // tonumber for example
results.push_back(something);
lua_pop(state,1); // stack restore
}
Run Code Online (Sandbox Code Playgroud)