[置顶] lua进阶8-- C++读取lua文件里的三维表

前面lua进阶里面讲到用C++读取2维表,但是最近又有了新的需求,lua文件里面出现了3维表,自己摸索了1下,终究找到了解决的方式,在这里面与大家分享1下</span>

我的lua文件里面的3维表结构以下

stage = {
{m_nId = 105,m_nBoss = 0,m_nScoreList = {300,450,600},m_nRewardList = {230401201,13020801,13010411},m_nDropItemList = {0,0,0}},
{m_nId = 106,m_nBoss = 0,m_nScoreList = {630,840,1050},m_nRewardList = {241801101,14000201,18000201},m_nDropItemList = {14000101,241700301,230501301}},
{m_nId = 107,m_nBoss = 0,m_nScoreList = {735,980,1225},m_nRewardList = {230400201,230400411,230500101},m_nDropItemList = {12020101,13010301,230401401}},
{m_nId = 108,m_nBoss = 0,m_nScoreList = {840,1120,1400},m_nRewardList = {19000101,241500901,18040201},m_nDropItemList = {14000401,230401201,14000101}},
{m_nId = 109,m_nBoss = 0,m_nScoreList = {1080,1440,1800},m_nRewardList = {12020301,230600101,241800501},m_nDropItemList = {16000401,230901101,230700301}},
{m_nId = 110,m_nBoss = 1,m_nScoreList = {1200,1600,2000},m_nRewardList = {0,0,220200901},m_nDropItemList = {230902101,231101101,231000401}}
};

读取代码以下

#include <stdio.h>
extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include <iostream>
#include <vector>
#include "string.h"
using namespace std;

int main ( int argc, char *argv[] )
{
lua_State* L;
L=lua_open();

luaopen_base(L);
luaopen_table(L);
luaopen_string(L);
luaopen_math(L);

if (luaL_dofile(L, "stage.lua"))
{
printf("run script failed
");
}
else
{
int it_idx;
int t_idx;

lua_getglobal(L,"stage");

t_idx=lua_gettop(L);
cout << t_idx << endl;
lua_pushnil(L);
while(lua_next(L,t_idx))
{
it_idx=lua_gettop(L);
lua_pushnil(L);
//cout << "it_idx = " << it_idx << endl;
int m_id = 0;
while(lua_next(L,it_idx))
{
const char* strkey = lua_tostring(L,⑵);
if (strcmp(strkey, "m_nId") == 0) {
m_id = (int)lua_tonumber(L, ⑴);
cout << "m_id = " <<m_id << " ";
}
if (strcmp(strkey, "m_nBoss") == 0) {
m_id = (int)lua_tonumber(L, ⑴);
cout << "m_nBoss = " << m_id << " ";
}

if (strcmp(strkey, "m_nScoreList") == 0)
{
if (lua_istable(L, ⑴))
{

cout << "m_nScoreList = " ;
int ll_idx = lua_gettop(L);
//cout << "ll_idx = " << ll_idx << endl;
lua_pushnil(L);
while (lua_next(L, ll_idx))
{
cout << (int)lua_tonumber(L, ⑴) << " ";
lua_pop(L, 1);
}

}
}

if (strcmp(strkey, "m_nRewardList") == 0)
{
if (lua_istable(L, ⑴))
{

cout << " m_nRewardList = " ;
int ll_idx = lua_gettop(L);
// cout << "ll_idx = " << ll_idx << endl;
lua_pushnil(L);
while (lua_next(L, ll_idx))
{
cout << (int)lua_tonumber(L, ⑴) <<" ";
lua_pop(L, 1);
}

}
}

if (strcmp(strkey, "m_nDropItemList") == 0)
{
if (lua_istable(L, ⑴))
{

cout << " m_nDropItemList = ";
int ll_idx = lua_gettop(L);
// cout << "ll_idx = " << ll_idx << endl;
lua_pushnil(L);
while (lua_next(L, ll_idx))
{
cout << (int)lua_tonumber(L, ⑴) << " ";
lua_pop(L, 1);
}

}
}

lua_pop(L,1);
}

cout << endl;

lua_pop(L,1);
//break;
}

}

lua_close(L);

return 0;

}

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » [置顶] lua进阶8-- C++读取lua文件里的三维表

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡