天天挂茶馆

挂茶馆易语言论坛

 找回密码
 立即注册
搜索
热搜: C++
查看: 3588|回复: 0

c++调用大漠系列:c++给LUA传递参数

[复制链接]
  • TA的每日心情
    郁闷
    2019-9-30 18:03
  • 签到天数: 33 天

    [LV.5]常住居民I

    25

    主题

    62

    帖子

    7576

    积分

    VIP会员

    Rank: 10Rank: 10Rank: 10

    积分
    7576
    发表于 2018-10-7 18:12:55 | 显示全部楼层 |阅读模式
    本帖最后由 1149 于 2018-10-7 21:08 编辑

    1,传递普通变量
    [mw_shl_code=cpp,true]int main(void)
    {
            lua_State *L = lua_open();
            lua_State *L1 = lua_open();
            luaopen_base(L);//加载基础库
            luaopen_string(L);//加载字符串库
            luaopen_table(L);//加载表处理库
            lua_register(L, "test", test);
            
            lua_pushstring(L, "全局变量值");//在堆栈里push一个值
            lua_setglobal(L, "name");//从堆栈上弹出一个值,并将其设为全局变量 name 的新值。
            
            if (luaL_loadfile(L, "main.lua"))
            {
                   
                    printf("加载错误:%s\n", lua_tostring(L, -1));
            }


            if (lua_pcall(L, 0, 0, 0))
            {
                    printf("调用错误:%s\n", lua_tostring(L, -1));
            }

            getchar();

            
            return 0;
    }[/mw_shl_code]



    2,传递表
    [mw_shl_code=cpp,true]int main(void)
    {
            lua_State *L = lua_open();
            luaopen_base(L);//加载基础库
            luaopen_string(L);//加载字符串库
            luaopen_table(L);//加载表处理库
            lua_register(L, "test", test);
           
            lua_newtable(L);//创建一个表
           
            lua_pushnumber(L, 1);
            lua_pushnumber(L, 22);
            lua_settable(L, -3);
           
            lua_pushnumber(L, 2);
            lua_pushnumber(L, 33);
            lua_settable(L, -3);
           
            lua_setglobal(L, "name");//将你创建的表设置为全局变量
           
            if (luaL_loadfile(L, "main.lua"))
            {
                   
                    printf("加载错误:%s\n", lua_tostring(L, -1));
            }


            if (lua_pcall(L, 0, 0, 0))
            {
                    printf("调用错误:%s\n", lua_tostring(L, -1));
            }

            getchar();

           
            return 0;
    }[/mw_shl_code]


    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版| 蜀ICP备15031375号 川公网安备51030402000199号

    GMT+8, 2025-5-9 20:55 , Processed in 0.322886 second(s), 9 queries , File On.

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表