关于C解析lua表结构的问题

燕雀安知鱼之乐 2016-05-06 05:03:01
我在lua程序中创建了一个表,表中存放的是表成员,想在C中读取表成员的成员,表的结构如下

local req = {};
req.opcode = GAME_OPCODE.CLI_PLAYCARD_REQ;
req.sSerialID = 1024;
req.nCardsCount = 1;
cards = {}
cards[1] = {}
cards[1].color = 0;
cards[1].value = 1;
cards[2] = {}
cards[2].color = 1;
cards[2].value = 3;
req.cards = cards;
sendToSdk(req)

我在C中获取协议要发送的信息

CCard* getCardsFlied(lua_State *L, const char *szKey)
{ OutputDebugString("join getCardsFlied ~~~~");
CCard cards[20];
lua_pushstring(L , szKey); //这个压入的的就是上面的req.cards
lua_gettable(L , -2);
int n = lua_objlen(L , -1);
for (int i = 0; i < n; i ++)
{
lua_rawgeti(L,1,i + 1);
char buf[20];
sprintf(buf,"\n%d",i +1);
OutputDebugString(buf);
lua_pop(L , 1);

}
return cards;

}

程序中得到的是card表,我想获取card表的成员color和value的值,请问我该怎么做
求大神帮忙,谢谢
...全文
126 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
我在脚本之家上看到相关基础http://www.jb51.net/article/55096.htm 自己试验了一下,好像解决了

CCard* getCardsFlied(lua_State *L, const char *szKey)
{
	CCard cards[20];
	lua_pushstring(L, szKey);
	lua_gettable(L,-2);
	for(int i = 0; i < 20; i ++)
	{
		//初始化卡牌属性
	  
		cards[i].m_nColor = -1;
		cards[i].m_nValue = -1;

		//压人对应的卡牌Key值
		lua_pushnumber(L,i + 1);
		lua_gettable(L , -2);
		if (lua_istable(L,-1))
		{
			
			
			//压人卡牌参数"color"
			lua_pushstring(L , "color");
			lua_gettable(L , -2);
			cards[i].m_nColor = lua_tonumber(L , -1);
			lua_pop(L , 1);

			//压人卡牌参数"value"
			lua_pushstring(L , "value");
			lua_gettable(L , -2);
			cards[i].m_nValue = lua_tonumber(L , -1);
			lua_pop(L , 1);
		}
		lua_pop(L , 1);


#ifdef MB_OS_WIN
		char buf[50];
		sprintf(buf,"\n\t cards[%d]:(%d , %d)",i,cards[i].m_nColor,cards[i].m_nValue);
		OutputDebugString(buf);
#endif

	}


	return cards;
}
保存一下,做个笔记

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧