@H_301_29@ copy //定义函数(返回table) int func_return_table(lua_State *L) { lua_newtable(L);//创建一个表格,放在栈顶 lua_pushstring(L, "mydata");//压入key lua_pushnumber(L,66);//压入value lua_settable(L,-3);//d出key,value,并设置到table里面去 "subdata");//压入value,也是一个table //压入subtable的key //value //这时候父table的位置还是-3,d出key,value(subtable),并设置到table里去 lua_pushstring(L, "mydata2");//同上 lua_pushnumber(L,77); lua_settable(L,-3); return 1;//堆栈里现在就一个table.其他都被d掉了。 } 输出结构体:
@H_301_29@
这里算是一个嵌套的结构体了。@H_301_29@ 函数解释:
@H_301_29@
就是把表在lua堆栈中的值d出来,index 是table 在堆栈中的位置,假如 table 在 -3,则key 应该是 -2,value 是 -1@H_301_29@
相当于 table[key] = value.@H_301_29@
例二:@H_301_29@
在工作中这里我想用一个类似数组的table,类似 tableaname[n].x@H_301_29@
代码:@H_301_29@
@H_301_29@ copy struct BattleResultdamageData { Uint32 nIcon; Uint32 ndamage; Uint16 nPetPos; Uint16 nLv; Uint8 nStar; Uint8 nEvo; Uint8 nCurSkin; Uint8 nStage; };
@H_301_29@ 打印的buffAtk这个table结构体信息:
copy <Info>0409-210505 654 Lookup local var buffAtk <Info>0409-210505 654 name = buffAtk type = table value = 0CB685A0 <Info>0409-210505 654 name = buffAtk[1] type = table value = 06CCEA58 <Info>0409-210505 670 name = buffAtk[1][dmg] type = number value = 89298 <Info>0409-210505 670 name = buffAtk[1][skin] type = number value = 0 <Info>0409-210505 670 name = buffAtk[1][star] type = number value = 6 <Info>0409-210505 689 name = buffAtk[1][evo] type = number value = 10 <Info>0409-210505 692 name = buffAtk[1][ID] type = number value = 25165827 <Info>0409-210505 693 name = buffAtk[1][lv] type = number value = 131 <Info>0409-210505 693 name = buffAtk[1][icon] type = number value = 1090 <Info>0409-210505 693 name = buffAtk[1][pos] type = number value = 19 <Info>0409-210505 693 name = buffAtk[2] type = table value = 0CC8C038 <Info>0409-210505 693 name = buffAtk[2][dmg] type = number value = 28726 <Info>0409-210505 693 name = buffAtk[2][skin] type = number value = 0 <Info>0409-210505 710 name = buffAtk[2][star] type = number value = 6 <Info>0409-210505 711 name = buffAtk[2][evo] type = number value = 12 <Info>0409-210505 711 name = buffAtk[2][ID] type = number value = 25165828 <Info>0409-210505 711 name = buffAtk[2][lv] type = number value = 90 <Info>0409-210505 711 name = buffAtk[2][icon] type = number value = 1325 <Info>0409-210505 711 name = buffAtk[2][pos] type = number value = 51 例三:
copy local buffAtk, buffDef= _G.getBattleResultData(); 打印这两个table的信息:
copy @H_419_769@ <Info>0409-212740 450 Lookup local var buffDef <Info>0409-212740 451 name = buffDef type = table value = 0CDD1EB8 <Info>0409-212740 451 name = buffDef[1] type = table value = 0CEEFCA8 <Info>0409-212740 451 name = buffDef[1][dmg] type = number value = 1992 <Info>0409-212740 451 name = buffDef[1][skin] type = number value = 0 <Info>0409-212740 466 name = buffDef[1][star] type = number value = 1 <Info>0409-212740 466 name = buffDef[1][evo] type = number value = 4 <Info>0409-212740 466 name = buffDef[1][ID] type = number value = 25165829 <Info>0409-212740 482 name = buffDef[1][lv] type = number value = 85 <Info>0409-212740 482 name = buffDef[1][icon] type = number value = 1005 <Info>0409-212740 482 name = buffDef[1][pos] type = number value = 2 <Info>0409-212740 482 name = buffDef[2] type = table value = 0CEED4D0 <Info>0409-212740 482 name = buffDef[2][dmg] type = number value = 2941 <Info>0409-212740 482 name = buffDef[2][skin] type = number value = 0 <Info>0409-212740 499 name = buffDef[2][star] type = number value = 2 <Info>0409-212740 499 name = buffDef[2][evo] type = number value = 6 <Info>0409-212740 499 name = buffDef[2][ID] type = number value = 25165830 <Info>0409-212740 499 name = buffDef[2][lv] type = number value = 85 <Info>0409-212740 499 name = buffDef[2][icon] type = number value = 1075 <Info>0409-212740 499 name = buffDef[2][pos] type = number value = 16 <Info>0409-212740 499 name = buffDef[3] type = table value = 0D0E83A0 <Info>0409-212740 516 name = buffDef[3][dmg] type = number value = 308 <Info>0409-212740 519 name = buffDef[3][skin] type = number value = 1 <Info>0409-212740 521 name = buffDef[3][star] type = number value = 0 <Info>0409-212740 523 name = buffDef[3][evo] type = number value = 6 <Info>0409-212740 523 name = buffDef[3][ID] type = number value = 25165831 <Info>0409-212740 523 name = buffDef[3][lv] type = number value = 85 <Info>0409-212740 523 name = buffDef[3][icon] type = number value = 1080 <Info>0409-212740 523 name = buffDef[3][pos] type = number value = 17 <Info>0409-212740 523 name = buffDef[4] type = table value = 0CD85A00 <Info>0409-212740 523 name = buffDef[4][dmg] type = number value = 792 <Info>0409-212740 539 name = buffDef[4][skin] type = number value = 0 <Info>0409-212740 539 name = buffDef[4][star] type = number value = 0 <Info>0409-212740 539 name = buffDef[4][evo] type = number value = 0 <Info>0409-212740 539 name = buffDef[4][ID] type = number value = 25165832 <Info>0409-212740 539 name = buffDef[4][lv] type = number value = 85 <Info>0409-212740 539 name = buffDef[4][icon] type = number value = 1145 <Info>0409-212740 539 name = buffDef[4][pos] type = number value = 30 <Info>0409-212740 554 name = buffDef[5] type = table value = 0C7CF7F8 <Info>0409-212740 554 name = buffDef[5][dmg] type = number value = 1608 <Info>0409-212740 554 name = buffDef[5][skin] type = number value = 0 <Info>0409-212740 554 name = buffDef[5][star] type = number value = 2 <Info>0409-212740 554 name = buffDef[5][evo] type = number value = 4 <Info>0409-212740 554 name = buffDef[5][ID] type = number value = 25165833 <Info>0409-212740 571 name = buffDef[5][lv] type = number value = 47 <Info>0409-212740 574 name = buffDef[5][icon] type = number value = 1090 <Info>0409-212740 577 name = buffDef[5][pos] type = number value = 19 <Info>0409-212740 578
看到这,是不是感觉哪里不对?是不是感觉代码好臃肿?@H_301_29@
下面是最最终优化后的代码:@H_301_29@
这下是不是感觉一下子少了好多?这就是代码的重用性! 总结
以上是内存溢出为你收集整理的Lua: 在C++中将table如何传入到lua中全部内容,希望文章能够帮你解决Lua: 在C++中将table如何传入到lua中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)