c#解析lua-Table类型

                LuaInterface.LuaTable ob = XLuaManager.Inst.Call("CombineDef.GetCombineItems", mItemType, mItemId)[0] as LuaInterface.LuaTable;
                
                XTools.Log("**** --- {0}", ob.Values.Count); //ob.Values[0];
//                 IEnumerator itr = ob.Keys.GetEnumerator();
//                 while(itr.MoveNext())
//                 {
//                     //KeyValuePair<XSkillType, int> pair = (KeyValuePair<XSkillType, int>)itr.Current;
//                     string nnn = (string)itr.Current;
//                     XTools.Log("---{0}", nnn);
//                 }
                IEnumerator itr = ob.Values.GetEnumerator();
                int i = 0;
                while(itr.MoveNext())
                {
                    LuaInterface.LuaTable tb = itr.Current as LuaInterface.LuaTable;
                    IEnumerator it2 = tb.Values.GetEnumerator();
                    XTools.Log("=={0}---key{1}", tb.Values.Count, i);
                    int j = 0;
                    while (it2.MoveNext())
                    {
                        //KeyValuePair<int, int> pair = (KeyValuePair<int, int>)it2.Current;
                        XTools.Log("---{0},{1}--------key:{2}{3}", it2.Current.GetType().ToString(), (double)it2.Current, i, ++j);
                    }
                    i++;
                }



 LuaInterface.LuaTable ob = XLuaManager.Inst.Call("CombineDef.GetCombineItems", mItemType, mItemId)[0] as string;

http://book.luaer.cn/

lua定义结构体:

Npc.BubbleProperty = 
{
Silence = { Weight = 200 }, -- 沉默
Task = { Weight = 50, nIndex = "Task" }, -- 普通泡泡
Normal = { Weight = 50, nIndex = "Normal" }, -- 任务泡泡
};


local tbPos =
{
[1] = { nX = nX, nY = nY };
[2] = { nX = nX - nSpan, nY = nY };
[3] = { nX = nX + nSpan, nY = nY };
[4] = { nX = nX, nY = nY - nSpan };
[5] = { nX = nX, nY = nY + nSpan };
[6] = { nX = nX - nSpan, nY = nY + nSpan };
[7] = { nX = nX + nSpan, nY = nY + nSpan };
[8] = { nX = nX + nSpan, nY = nY - nSpan };
[9] = { nX = nX - nSpan, nY = nY - nSpan };
};


详细描述Lua和C之间相互传递Table类型数据 /* ====================================================== */ // 遍历Lua传入的Table类型参数, 获取它的Key/Value, 其关键操作是 lua_next() // lua_next() 返回1示读取成功,返回0示已经没有数据可读了 // lua_next() 会使用栈顶元素作为Key去定位本次需要取出Table里面的那个值对 // 如果Key=nil, 那就示本次取出的是第一个元素 // 它会先将当前的这个Key弹出,然后将本次取出的Key/Value压入栈, Value在栈顶 // 一个比较隐晦的处理就是, 我们不应直接使用lua_tostring(L, -2)来读取Key // 因为lua_tostring()在Key类型不是字符串时, 它会修改栈上的Key数据 // 这样, 下次调用lua_next()时, 就会因为Key被修改了而导致错误 // 为此,先调用lua_pushvalue(L, -2),将它Copy一份到栈顶,对这个Copy进行lua_tostring() // 读取Key,Value到C变量里面后,将Value和Copy弹出,留着Key在栈顶,给下次lua_next()用 // // 指令及栈图变化如下: (假如Table的栈下标是Index) // 0. 刚进入函数时 ...Table, ... <--- 这里栈顶 // 1. lua_pushnil(L) ...Table, ..., nil <--- 这里栈顶 // 2. lua_next(L, Index) ...Table, ..., Key, Value <--- 这里栈顶 // 3. lua_pushvalue(L, -2) ...Table, ..., Key, Value, KeyCopy <--- 这里栈顶 // 4. lua_pop(L, 2), ...Table, ..., Key <--- 这里栈顶 // ... 如此重复2,3,4 // N. lua_next(L, Index)返回0 ...Table, ... <--- 这里栈顶 /* ====================================================== */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值