下面的代码演示了在C++和lua脚本之间传递数据。
首先在C++中创建一个table,添加元素,然后放置到lua的全局表中。在lua脚本中可以使用C++创建的这个表。
然后在脚本中创建一个表,以脚本返回值的方式返回给C++,在C++中可以读取表中的值。
例子代码需要一个args.lua的lua文件,要手工创建,我把它放到了C盘根目录下。
-
-
-
- #include "stdafx.h"
-
- extern "C"
- {
- #include
- #include
- #include
- }
- #include
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- int status;
-
-
- lua_State* state = lua_open();
-
-
-
-
-
-
-
-
-
-
-
- luaL_openlibs(state);
-
- status = luaL_loadfile( state, "c://args.lua" );
-
- std::cout << "[C++] Passing 'arg' array to script" << std::endl;
-
-
- lua_newtable( state );
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- lua_pushnumber( state, 1 );
- lua_pushnumber( state, 45 );
- lua_rawset( state, -3 );
-
-
- lua_pushnumber( state, 2 );
- lua_pushnumber( state, 99 );
- lua_rawset( state, -3 );
-
-
-
-
- lua_pushliteral( state, "n" );
- lua_pushnumber( state, 2 );
- lua_rawset( state, -3 );
-
-
-
-
- lua_setglobal( state, "arg" );
-
-
- std::cout << "[C++] Running script" << std::endl;
-
- int result = 0;
- if (status == 0)
- {
- result = lua_pcall( state, 0, LUA_MULTRET, 0 );
- }
- else
- {
- std::cout << "bad" << std::endl;
- }
-
- if (result != 0)
- {
- std::cerr << "[C++] script failed" << std::endl;
- }
-
- std::cout << "[C++] These values were returned from the script" << std::endl;
-
-
-
- while (lua_gettop( state ))
- {
- switch (lua_type( state, lua_gettop( state ) ))
- {
- case LUA_TNUMBER:
- {
- std::cout << "script returned " << lua_tonumber( state, lua_gettop( state ) ) << std::endl;
- break;
- }
- case LUA_TTABLE:
- {
- std::cout << "script returned a table" << std::endl;
-
-
-
-
-
- lua_pushnil(state);
- int t = -2;
- while (lua_next(state, t) != 0)
- {
-
- const char* key = "unknown";
- const char* value;
- if(lua_type(state, -2) == LUA_TSTRING)
- {
- key = lua_tostring(state, -2);
- value = lua_tostring(state, -1);
- }
- else if(lua_type(state, -2) == LUA_TNUMBER)
- {
-
-
-
- lua_pushvalue(state, -2);
- key = lua_tostring(state, -1);
- lua_pop(state, 1);
- value = lua_tostring(state, -1);
- }
- else
- {
- value = lua_tostring(state, -1);
- }
-
- std::cout <<"key="<< key
- << ", value=" << value << std::endl;
-
-
- lua_pop(state, 1);
- }
-
- break;
- }
- case LUA_TSTRING:
- {
- std::cout << "script returned " << lua_tostring( state, lua_gettop( state ) ) << std::endl;
- break;
- }
- case LUA_TBOOLEAN:
- {
- std::cout << "script returned " << lua_toboolean( state, lua_gettop( state ) ) << std::endl;
- break;
- }
- default:
- std::cout << "script returned unknown param" << std::endl;
- break;
- }
- lua_pop( state, 1 );
- }
- lua_close( state );
- return 0;
- }
本例用了一个控制台工程,输出如下:
[C++] Passing 'arg' array to script
[C++] Running script
[lua] These args were passed into the script from C
1 45
2 99
[lua] Script returning data back to C
1 9
2 8
3 7
4 6
5 5
6 test 6
test 99 99
test1 key test1 value
[C++] These values were returned from the script
script returned 1
script returned 9
script returned a table
key=1, value=9
key=2, value=8
key=3, value=7
key=4, value=6
key=5, value=5
key=6, value=test 6
key=test 99, value=99
key=test1 key, value=test1 value