智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...
随笔 - 991, 文章 - 0, 评论 - 27, 阅读 - 341万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Lua C++互传结构体实例

Posted on   Bill Yuan  阅读(3917)  评论(0编辑  收藏  举报

转自:http://bbs.csdn.net/topics/350261649

复制代码
=====main.cpp=======
#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==========
function PlayOperate(x)
    x.wChairID = x.wChairID+1
    x.iHeroID = x.iHeroID+1
    x.iChosenHeros[0]= 9
    x.iChosenHeros[1]= 10
    
    return x
end
复制代码

 

(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2014-08-07 cocos2dx 网络编程(CCHttpRequest和CURL两个方式)
2014-08-07 cocos2dx json数据解析
2014-08-07 cocos2dx Http网络编程
2014-08-07 Android平台NDK编程
2014-08-07 cocos2dx 屏幕适配策略
2012-08-07 Unity开发项目的一点经验
2012-08-07 Unity粒子系统
点击右上角即可分享
微信分享提示