Lua C++互传结构体实例
转自: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[];
}
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, -);
lua_pushstring(L, "iHeroID");
lua_pushnumber(L, obj.iHeroID);
lua_settable(L, -); lua_pushstring(L, "iChosenHeros");
lua_newtable(L);
for (i=;i<;++i)
{
lua_pushnumber(L, i);
lua_pushnumber(L, obj.iChosenHeros[i]);
lua_settable(L, -);
}
lua_settable(L, -); lua_call(L, , ); lua_pushstring(L, "wChairID");
int n=lua_gettop(L);
lua_gettable(L, -);
obj.wChairID = (int)lua_tonumber(L, -);
lua_pop(L, );
lua_pushstring(L, "iHeroID");
lua_gettable(L, -);
obj.iHeroID = (int)lua_tonumber(L, -);
lua_pop(L, );
lua_pushstring(L, "iChosenHeros");
lua_gettable(L, -);
for (i=;i<;++i)
{
lua_pushnumber(L, i);
lua_gettable(L, -);
obj.iChosenHeros[i]=(int)lua_tonumber(L, -);
lua_pop(L, );
} } int main(int argc, char *argv[])
{
int i;
Player obj; obj.wChairID = ;
obj.iHeroID = ; for(i=; i<; ++i)
obj.iChosenHeros[i]=; //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=; i<; ++i)
printf( "obj.iChosenHeros[%d] = %d\n", i, obj.iChosenHeros[i]); /* initialize Lua */
L = lua_open();
if (NULL == L)
{
return -;
}
/* 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=; i<; ++i)
printf( "obj.iChosenHeros[%d] = %d\n", i, obj.iChosenHeros[i]); /* cleanup Lua */
lua_close(L); return ; } =============aaa.lua==========
function PlayOperate(x)
x.wChairID = x.wChairID+
x.iHeroID = x.iHeroID+
x.iChosenHeros[]=
x.iChosenHeros[]= return x
end
Lua C++互传结构体实例的更多相关文章
- ctypes 操作 python 与 c++ dll 互传结构体指针
CMakeLists.txt # project(工程名) project(blog-3123958139-1) # add_library(链接库名称 SHARED 链接库代码) add_libra ...
- C#调用C++系列二:传结构体
这一篇记录下C#调用C++的结构体的方式来使用OpenCV的数据格式,这里会有两种方式,第一种是C#传一个结构体和图像的路径给C++,然后C++将图像加载进来,再把传进来的结构体填满即可,第二种是C# ...
- C#中使用反射获取结构体实例
一般用反射获取类对象的实例比较简单,只要类有一个无参构造函数或没有显示声明带参的构造函数即可使用如下代码 static void Main(string[] args) { Type type = t ...
- Qt socket中怎么传结构体?
直接发送和接收结构体,例如:struct A {...};struct A objectA; 发送的时候: tcpSocket->write((char *)&objectA, size ...
- C语言结构体实例-创建兔子
参考裸编程思想. #include <stdio.h> //#include "ycjobject.h" // 颜色定义 #define CL_BLACK 0 #def ...
- C结构体中数据的内存对齐问题
转自:http://www.cnblogs.com/qwcbeyond/archive/2012/05/08/2490897.html 32位机一般默认4字节对齐(32位机机器字长4字节),64位机一 ...
- 转载 C#结构体(struct)和类(class)的区别
转载原地址: http://dotnet.9sssd.com/csbase/art/8 C#结构体和类的区别问题:在C#编程语言中,类属于引用类型的数据类型,结构体属于值类型的数据类型,这两种数据类型 ...
- 深入理解C指针之六:指针和结构体
原文:深入理解C指针之六:指针和结构体 C的结构体可以用来表示数据结构的元素,比如链表的节点,指针是把这些元素连接到一起的纽带. 结构体增强了数组等集合的实用性,每个结构体可以包含多个字段.如果不用结 ...
- Swift 结构体的使用
Swift 结构体是构建代码所用的一种通用且灵活的构造体. 我们可以为结构体定义属性(常量.变量)和添加方法,从而扩展结构体的功能. 与 C 和 Objective C 不同的是: 结构体不需要包含实 ...
随机推荐
- Ansible之roles介绍
本节内容: 什么场景下会用roles? roles示例 一.什么场景下会用roles? 假如我们现在有3个被管理主机,第一个要配置成httpd,第二个要配置成php服务器,第三个要配置成MySQL服务 ...
- pdo 数据库链接
在PHP中,我们还可以使用一种更为简单直接的数据库连接方案——PDO持久化连接. 关于PDO本身,这里就不再多作介绍了,大家可以参考之前的文章<使用PDO连接多种数据库>以及PHP官方网站 ...
- Axure使用笔记1:如何去除IE中每次“已限制网页运行脚本或ActiveX控件”
每次在Axure中画原型预览的时候,IE每次都有 这个比较烦,在Internent做如下设置,即可不再烦恼 看到没,给允许活动内容在我的计算机上的文件中运行打上勾
- 关闭所有的screen
由于开了很多个screen同时工作,关闭是一个一个比较麻烦,写个命令在这以便日后想不起来时可以用到.1.先看看有多少个screen screen -ls |awk '/Socket/'|awk '{p ...
- HDU1042N!大数的阶乘java模板
import java.math.BigInteger; import java.util.Scanner; public class Main{ public static void main(St ...
- Object Tracking Benchmark
Abstract 问题: 1)evaluation is often not suffcient 2)biased for certain types of algorthms 3)datasets ...
- FREESWITCH 填坑指南
转接 1.查看网关注册状态 sofia status 2.桥接(未实践) http://wiki.freeswitch.org.cn/wiki/Mod_lua.html#jump10237 frees ...
- gettimeofday的使用
前言 好像只能在linux平台使用,在windows平台不能使用.... #include <sys/time.h> long cur_tp, cost_tp, tmp_tp; struc ...
- .NET 中使用 Mutex 进行跨越进程边界的同步
Mutex 是 Mutual Exclusion 的缩写,是互斥锁,用于防止两个线程同时对计算机上的同一个资源进行访问.不过相比于其他互斥的方式,Mutex 能够跨越线程边界. 本文内容 Mutex ...
- hive中实现类似MySQL中的group_concat功能
hive> desc t; OK id string str string Time taken: 0.249 seconds hive> select * from t ...