lua userdata

想把C的结构

  1. struct Player
  2. {
  3. WORD wChairID;int iHeroID; int iChosenHeros[16];
  4. };

如上 传给lua

  1. #include "stdio.h"
  2. extern "C"
  3. {
  4. #include "lua/lua.h"
  5. #include "lua/lualib.h"
  6. #include "lua/lauxlib.h"
  7. };
  8. typedef struct
  9. {
  10. int wChairID;
  11. int iHeroID;
  12. int iChosenHeros[16];
  13. }
  14. Player;
  15. /* LUA接口声明*/
  16. lua_State* L;
  17. void Operate(Player &obj)
  18. {
  19. int i;
  20. lua_getglobal(L, "PlayOperate");
  21. lua_newtable(L);
  22. lua_pushstring(L, "wChairID");
  23. lua_pushnumber(L, obj.wChairID);
  24. lua_settable(L, -3);
  25. lua_pushstring(L, "iHeroID");
  26. lua_pushnumber(L, obj.iHeroID);
  27. lua_settable(L, -3);
  28. lua_pushstring(L, "iChosenHeros");
  29. lua_newtable(L);
  30. for (i=0;i<16;++i)
  31. {
  32. lua_pushnumber(L, i);
  33. lua_pushnumber(L, obj.iChosenHeros[i]);
  34. lua_settable(L, -3);
  35. }
  36. lua_settable(L, -3);
  37. lua_call(L, 1, 1);
  38. lua_pushstring(L, "wChairID");
  39. int n=lua_gettop(L);
  40. lua_gettable(L, -2);
  41. obj.wChairID = (int)lua_tonumber(L, -1);
  42. lua_pop(L, 1);
  43. lua_pushstring(L, "iHeroID");
  44. lua_gettable(L, -2);
  45. obj.iHeroID = (int)lua_tonumber(L, -1);
  46. lua_pop(L, 1);
  47. lua_pushstring(L, "iChosenHeros");
  48. lua_gettable(L, -2);
  49. for (i=0;i<16;++i)
  50. {
  51. lua_pushnumber(L, i);
  52. lua_gettable(L, -2);
  53. obj.iChosenHeros[i]=(int)lua_tonumber(L, -1);
  54. lua_pop(L, 1);
  55. }
  56. }
  57. int main(int argc,char *argv[])
  58. {
  59. int i;
  60. Player obj;
  61. obj.wChairID = 1;
  62. obj.iHeroID = 2;
  63. for(i=0; i<16; ++i)
  64. obj.iChosenHeros[i]=3;
  65. //print initial value
  66. printf( "The origin is blow:\n");
  67. printf( "obj.wChairID = %d\n", obj.wChairID);
  68. printf( "obj.iHeroID = %d\n", obj.iHeroID);
  69. for(i=0; i<16; ++i)
  70. printf( "obj.iChosenHeros[%d] = %d\n", i, obj.iChosenHeros[i]);
  71. /* initialize Lua */
  72. L = lua_open();
  73. if (NULL == L)
  74. {
  75. return -1;
  76. }
  77. /* load Lua base libraries */
  78. luaL_openlibs(L);
  79. /* load the script */
  80. luaL_dofile(L, "e:\\aaa.lua"); //这里指定aaa.lua文件的位置
  81. /* call function */
  82. Operate(obj);
  83. /* print the result */
  84. printf( "The result is blow:\n");
  85. printf( "obj.wChairID = %d\n", obj.wChairID);
  86. printf( "obj.iHeroID = %d\n", obj.iHeroID);
  87. for(i=0; i<16; ++i)
  88. printf( "obj.iChosenHeros[%d] = %d\n", i, obj.iChosenHeros[i]);
  89. /* cleanup Lua */
  90. lua_close(L);
  91. return 0;
  92. }
#include "stdio.h"

extern "C"
{
#include "lua/lua.h"
#include "lua/lualib.h"
#include "lua/lauxlib.h"
};



typedef struct 
{
int  wChairID;
int  iHeroID;
int  iChosenHeros[16];
}
Player;

/* LUA接口声明*/
lua_State* L;

void Operate(Player &obj)
{
int i;

lua_getglobal(L, "PlayOperate");

    lua_newtable(L);
lua_pushstring(L, "wChairID");
    lua_pushnumber(L, obj.wChairID);
lua_settable(L, -3);
    lua_pushstring(L, "iHeroID");
lua_pushnumber(L, obj.iHeroID);
lua_settable(L, -3);

lua_pushstring(L, "iChosenHeros");
lua_newtable(L);
for (i=0;i<16;++i)
{
lua_pushnumber(L, i);
lua_pushnumber(L, obj.iChosenHeros[i]);
lua_settable(L, -3);
}
    lua_settable(L, -3);

    lua_call(L, 1, 1);

lua_pushstring(L, "wChairID");
int n=lua_gettop(L);
lua_gettable(L, -2);
obj.wChairID = (int)lua_tonumber(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "iHeroID");
lua_gettable(L, -2);
obj.iHeroID = (int)lua_tonumber(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "iChosenHeros");
lua_gettable(L, -2);
for (i=0;i<16;++i)
{
lua_pushnumber(L, i);
lua_gettable(L, -2);
obj.iChosenHeros[i]=(int)lua_tonumber(L, -1);
lua_pop(L, 1);
}

}

int main(int argc, char *argv[])
{
int i;
    Player obj;

obj.wChairID = 1;
obj.iHeroID  = 2;

for(i=0; i<16; ++i)
obj.iChosenHeros[i]=3;

//print initial value
printf( "The origin is blow:\n");
printf( "obj.wChairID = %d\n", obj.wChairID);
printf( "obj.iHeroID = %d\n", obj.iHeroID);
for(i=0; i<16; ++i)
printf( "obj.iChosenHeros[%d] = %d\n", i, obj.iChosenHeros[i]);


/* initialize Lua */
L = lua_open();
    if (NULL == L)
    {
return -1;
    }
/* load Lua base libraries */
luaL_openlibs(L);

/* load the script */
luaL_dofile(L, "e:\\aaa.lua");  //这里指定aaa.lua文件的位置

/* call function */
Operate(obj);

/* print the result */
printf( "The result is blow:\n");
printf( "obj.wChairID = %d\n", obj.wChairID);
printf( "obj.iHeroID = %d\n", obj.iHeroID);
for(i=0; i<16; ++i)
printf( "obj.iChosenHeros[%d] = %d\n", i, obj.iChosenHeros[i]);

/* cleanup Lua */
lua_close(L);

return 0;

}


=============aaa.lua==========

  1. function PlayOperate(x)
  2. x.wChairID = x.wChairID+1
  3. x.iHeroID = x.iHeroID+1
  4. x.iChosenHeros[0]= 9
  5. x.iChosenHeros[1]= 10
  6. return x
  7. end  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值