C++将结构体传给lua
2016-03-24 16:11
656 查看
简单概要
就是将结构体传给lua。代码
C++代码[cpp] view
plain copy
print?
void CProject1Dlg::OnBnClickedButton2()
{
// 打开换为 luaL_newstate
lua_State *L = luaL_newstate() ; /* 打开 Lua */
luaL_openlibs(L); /* 加载 .lib 文件 */
// 加载脚本文件,需要放在程序目录
luaL_loadfile( L, "test.lua" );
lua_resume( L, 0 , 0);
typedef struct
{
int a;
CString strTest;
struct test
{
int b;
}c;
}A;
A a;
a.a = 10;
a.c.b = 322;
a.strTest = _T("yes");
lua_getglobal(L,"structTest");
lua_newtable(L);
lua_pushinteger(L,a.a);
lua_setfield(L,-2,"a");
lua_newtable(L);
lua_pushinteger(L,a.c.b);
lua_setfield(L,-2,"b");
lua_setfield(L,-2,"c");
lua_pushstring(L,CStringA(a.strTest).GetString());
lua_setfield(L,-2,"strTest");
lua_pcall(L,1,1,NULL); //
// 输出计算结果
CString c = CString(lua_tostring(L,-1)) ;
lua_pop(L,1) ; // 清除堆栈 清除计算结果
// 调用结束
lua_close(L);
MessageBox(c);
}
Lua代码
[plain] view
plain copy
print?
function structTest(a)
return string.format("做个测试 \r\n a.a = %d \n a.b = %d \n a.strTest = %s",
a.a,a.c.b,a.strTest);
end
运行结果
相关文章推荐
- Lua和C++语言的交互详解
- LUA 怎么理解coroutine ?
- lua脚本入门
- lua入门之二:c/c++ 调用lua及多个函数返回值的获取
- Lua和C++交互详细总结
- Lua_第18 章 Table 库
- Lua_第17 章 数学库
- lua安装和简单测试
- Lua垃圾回收
- lua绑定
- Lua简明教程
- lua常用函数
- Dorothy Lua开发建议
- 基于 lua 的热更新系统设计要点
- 167.Evaluate the following DELETE statement:
- 战火引擎中如何写Lua逻辑脚本
- Lua 中使用面向对象(续)--newproxy生成userdata使用__gc
- lua gc 优化方案
- lua入门系列
- 一步步学tolua++