想把C的结构
[cpp] view plaincopyprint?
- struct Player
- {
- WORD wChairID;int iHeroID; int iChosenHeros[16];
- };
如上 传给lua
[cpp] view plaincopyprint?
- #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_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,"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);
- }
-
- }
-
- 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;
-
- printf( "The origin is blow:n");
- printf( "obj.wChairID = %dn",obj.wChairID);
- printf( "obj.iHeroID = %dn",obj.iHeroID);
- for(i=0; i<16; ++i)
- printf( "obj.iChosenHeros[%d] = %dn",i,obj.iChosenHeros[i]);
-
-
- L = lua_open();
- if (NULL == L)
- {
- return -1;
- }
- luaL_openlibs(L);
-
- luaL_dofile(L, "e:aaa.lua");
-
- Operate(obj);
-
- printf( "The result is blow:n");
- printf( "obj.wChairID = %dn",obj.wChairID);
- printf( "obj.iHeroID = %dn",obj.iHeroID);
- for(i=0; i<16; ++i)
- printf( "obj.iChosenHeros[%d] = %dn",obj.iChosenHeros[i]);
-
- lua_close(L);
-
- return 0;
-
- }
#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);
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,"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);
}
}
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 = %dn",obj.wChairID);
printf( "obj.iHeroID = %dn",obj.iHeroID);
for(i=0; i<16; ++i)
printf( "obj.iChosenHeros[%d] = %dn",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 = %dn",obj.iChosenHeros[i]);
/* cleanup Lua */
lua_close(L);
return 0;
}
=============aaa.lua==========
[plain] view plaincopyprint?
- function PlayOperate(x)
- x.wChairID = x.wChairID+1
- x.iHeroID = x.iHeroID+1
- x.iChosenHeros[0]= 9
- x.iChosenHeros[1]= 10
-
- return x
- end ?
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|