C++和Lua相互传递数据

本篇文章主要介绍C++和Lua相互传递数据。如果你还不知道怎么在C/C++里面调用Lua脚本的话,请参考这篇文章。本文主要介绍基本数据类型的传递,比如整型(int),字符串(string)、数字(number)及bool值。


加载并运行Lua脚本

由于在上一个教程里面已经介绍过如何在C/C++里面嵌入Lua,所以这一节就简单的介绍一下程序怎么用,配置就略过啦。


创建Lua虚拟机

1
lua_State *lua_state = luaL_newstate();


加载Lua库

1
2
3
4
5
6
7
8
9
10
11
12
static  const  luaL_Reg lualibs[] =
     {
         { "base" , luaopen_base},
         { "io" , luaopen_io},
         {NULL, NULL}
     };
     const  luaL_Reg *lib = lualibs;
     for (; lib->func != NULL; lib++)
     {
         luaL_requiref(lua_state, lib->name, lib->func, 1);
         lua_settop(lua_state, 0);
     }


运行Lua脚本

1
2
3
4
5
6
7
8
9
10
11
12
std::string scriptPath = FileUtils::getInstance()->fullPathForFilename( "hello.lua" );
     int  status = luaL_loadfile(lua_state, scriptPath.c_str());
     std::cout <<  " return: "  << status << std::endl;
     int  result = 0;
     if (status == LUA_OK)
     {
         result = lua_pcall(lua_state, 0, LUA_MULTRET, 0);
     }
     else
     {
         std::cout <<  " Could not load the script."  << std::endl;
     }


这里我们使用的是luaL_loadfile而不是之前的luaL_dofile,其实luaL_dofile只是一个宏定义:

1
2
#define luaL_dofile(L, fn) \
     (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))

我们先调用luaL_loadfile可以判断Lua脚本是否加载成功,然后再调用lua_pcall来执行Lua脚本。


C/C++调用Lua函数

首先,我们在hello.lua里面定义一个Lua函数:

1
2
3
4
-- add two numbers
function add ( x, y )
     return  x + y
end

Lua的函数定义是以function为keyword,然后以end结尾,同时它的参数是没有形参类型的,另外,Lua的函数可以返回多个值。不过我们这里只返回了一个值。


接下来,让我们看看如果在C++程序里面调用这个函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int  luaAdd(lua_State *lua_state ,  int  x,  int  y)
{
     int  sum;
     //获取lua里面的add函数并把它放到lua的栈顶
     lua_getglobal(lua_state,  "add" );
     //往lua栈里面压入两个参数
     lua_pushnumber(lua_state, x);
     lua_pushnumber(lua_state, y);
     //调用lua函数,这里的2是参数的个数,1是返回值的个数
     lua_call(lua_state, 2, 1);
     //从栈顶读取返回值,注意这里的参数是-1
     sum = lua_tointeger(lua_state, -1);
     //最后我们把返回值从栈顶拿掉
     lua_pop(lua_state, 1);
     return  sum;
}


然后,我们就可以在程序里面调用它了:

1
std::cout<<  "2 + 1= "  << luaAdd(lua_state,4,1)<<std::endl;

注意,这个方法调用要在lua_pcall调用之后。


操作Lua全局变量

C++里面获取Lua全局变量的值

首先,我们在hello.lua里面定义一个全局变量

1
myname =  "子龙山人"

然后我们在C++里面访问它:

1
2
3
4
lua_getglobal(lua_state,  "myname" );
     std::string myname = lua_tostring(lua_state, -1);
     lua_pop(lua_state, 1);
     std::cout<< "Hello: " <<myname<<std::endl;

这一次我们又是通过lua_getglobal来把myname这个全局变量压到lua栈,然后用lua_tostring来取这个值。


C++里面修改Lua全局变量的值

这次我们使用的是lua_setglobal来传递数据给Lua:

1
2
  lua_pushstring(lua_state,  "World" );
  lua_setglobal(lua_state,  "myname" );

这时,我们只要在hello.lua的最开始部分,调用print(myname)就可以打印传递进来的值了。


C++传递Table给Lua

1
2
3
4
5
6
7
8
9
10
    lua_createtable(lua_state, 2, 0);
     lua_pushnumber(lua_state, 1);
     lua_pushnumber(lua_state, 49);
//    lua_settable(lua_state, -3);
     lua_rawset(lua_state, -3);
     lua_pushnumber(lua_state, 2);
     lua_pushstring(lua_state,  "Life is a beach" );
//    lua_settable(lua_state, -3);
     lua_rawset(lua_state, -3);
     lua_setglobal(lua_state,  "arg" );


这里我们传递了一个table给lua,这个table为{49,"Life is a beach"}。Lua table的索引是从1开始的,然后我们在lua脚本里面可以这样子来访问这个table:

1
2
3
for  i=1,#arg  do
     print( "      " , i, arg[i])
end


这里的#arg是获得table的长度,然后使用arg[i]来获取table的索引i处的value。


Lua返回多个值给C++

首先是Lua代码:

1
2
3
4
local temp = {9,  "hehehej" }
-- temp[1]=9
-- temp[2]= "See you space cowboy!"
return  temp,9,1


然后是C++代码:

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
  std::stringstream str_buf;
     while (lua_gettop(lua_state))
     {
         str_buf.str(std::string());
         str_buf <<  " " ;
         switch (lua_type(lua_state, lua_gettop(lua_state)))
         {
             case  LUA_TNUMBER:
                 str_buf <<  "script returned the number: "
                 << lua_tonumber(lua_state, lua_gettop(lua_state));
                 break ;
             case  LUA_TTABLE:
                 str_buf <<  "script returned a table" ;
                 break ;
             case  LUA_TSTRING:
                 str_buf <<  "script returned the string: "
                 << lua_tostring(lua_state, lua_gettop(lua_state));
                 break ;
             case  LUA_TBOOLEAN:
                 str_buf <<  "script returned the boolean: "
                 << lua_toboolean(lua_state, lua_gettop(lua_state));
                 break ;
             default :
                 str_buf <<  "script returned an unknown-type value" ;
         }
         lua_pop(lua_state, 1);
         std::cout << str_buf.str() << std::endl;
     }


最后输出结果为:

1
2
3
4
5
[C++] Values returned from the script:
  script returned the number: 1
  script returned the number: 9
  script returned a table
[C++] Closing the Lua state

在Lua里面return值的顺序是table,9,1,而在C++里面是倒过来的。因为我们是使用栈作为数据结构来传递数据,而栈是先进后出的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值