// 创建二维数组Lua表
void createLuaTable2(lua_State *L, const char* t)
{
lua_newtable(L); // t
//========================
lua_pushinteger(L, 1); // t[1]
lua_newtable(L); // p1
// p1[1] = 100
lua_pushinteger(L, 1);
lua_pushinteger(L, 100);
lua_settable(L, 3);
// p1[2] = 200
lua_pushinteger(L, 2);
lua_pushinteger(L, 200);
lua_settable(L, 3);
// p1[3] = 300
lua_pushinteger(L, 3);
lua_pushinteger(L, 300);
lua_settable(L, 3);
// t[1] = p1
lua_settable(L, 1);
//==========================
lua_pushinteger(L, 2); // t[2]
lua_newtable(L); // p2
// p2[1] = 'AAAA'
lua_pushinteger(L, 1);
lua_pushstring(L, "AAAA");
lua_settable(L, 3);
// p2[2] = 'BBBB'
lua_pushinteger(L, 2);
lua_pushstring(L, "BBBB");
lua_settable(L, 3);
// p2[3] = 'CCCC'
lua_pushinteger(L, 3);
lua_pushstring(L, "CCCC");
lua_settable(L, 3);
// t[2] = p2
lua_settable(L, 1);
//=========================
lua_setglobal(L, t); // 设置全局变量
}
/*
createLuaTable2(L, "aaa"); 等价于Lua代码:
-- 全局变量
aaa = {
{100,200,300},
{"AAAA", "BBBB", "CCCC"}
};
*/
在C中创建二维Lua表示例
最新推荐文章于 2021-05-22 06:08:04 发布