tolua++的最新版本是5.0,下载地址:http://www.tecgraf.puc-rio.br/~celes/tolua/

http://www.codenix.com/~tolua/#download
以下是简单的使用说明:

功能内容:可以在LUA脚本中使用C++的方便对象,包含创建,释放,调用成员函数
文件包括:Main.cpp,tClass.cpp,tClass.h,tClass.pkg,tClassLua.cpp(自动生成)

tClass.h(定义提供给LUA使用的类)
#ifndef TCLASS_H
#define TCLASS_H

// 简单的类
class A
{
public:
 int a;
 A( int nTemp );
 int Get();
};

#endif

tClass.cpp(类的简单实现)
#include "tClass.h"

A::A( int nTemp )
{
 a = nTemp;
}

int A::Get()
{
 return a;
}

tClass.pkg(重要的一环节)
tolua会根据tClass.pkg来自动生产tClassLua.cpp,tClass.pkg是手动编写的,而已格式也有规定

,以下是tClass.pkg内容:
$#include "tClass.h"

class A
{
 int a;
 A( int nTemp );
 int Get();
};

.pkg的书写格式与C++的格式差不多,是只没有大部分的关键字,例如:public等,注意,需要包函

tClass.h的头文件,格式是$#include "tClass.h",还有一点书注意的,由于类中的权限关键字

不存在,所以生成代码的时候可能会出现权限访问错误,例如在int a是私有的情况,可能生产的代

码会出问题。所以设计类的时候需要注意.

生成代码:在命令行中输入:tolua -o tClassLua.cpp,tClass.pkg
生成的代码
/*
** Lua binding: tClass
** Generated automatically by tolua 5.0a on 05/25/06 17:00:17.
*/

#ifndef __cplusplus
#include "stdlib.h"
#endif
#include "string.h"

#include "tolua.h"

/* Exported function */
TOLUA_API int tolua_tClass_open (lua_State* tolua_S);

#include "tClass.h"

/* function to register type */
static void tolua_reg_types (lua_State* tolua_S)
{
 tolua_usertype(tolua_S,"A");
}

/* get function: a of class  A */
static int tolua_get_A_A_a(lua_State* tolua_S)
{
  A* self = (A*)  tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
 if (!self) tolua_error(tolua_S,"invalid 'self' in accessing variable 'a'",NULL);
#endif
 tolua_pushnumber(tolua_S,(lua_Number)self->a);
 return 1;
}

/* set function: a of class  A */
static int tolua_set_A_A_a(lua_State* tolua_S)
{
  A* self = (A*)  tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
 tolua_Error tolua_err;
 if (!self) tolua_error(tolua_S,"invalid 'self' in accessing variable 'a'",NULL);
 if (!tolua_isnumber(tolua_S,2,0,&tolua_err))
 tolua_error(tolua_S,"#vinvalid type in variable assignment.",&tolua_err);
#endif
  self->a = ((int)  tolua_tonumber(tolua_S,2,0));
 return 0;
}

/* method: new of class  A */
static int tolua_tClass_A_new00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
 tolua_Error tolua_err;
 if (
 !tolua_isusertable(tolua_S,1,"A",0,&tolua_err) ||
 !tolua_isnumber(tolua_S,2,0,&tolua_err) ||
 !tolua_isnoobj(tolua_S,3,&tolua_err)
 )
 goto tolua_lerror;
 else
#endif
 {
  int nTemp = ((int)  tolua_tonumber(tolua_S,2,0));
 {
  A* tolua_ret = (A*)  new A(nTemp);
 tolua_pushusertype(tolua_S,(void*)tolua_ret,"A");
 }
 }
 return 1;
#ifndef TOLUA_RELEASE
 tolua_lerror:
 tolua_error(tolua_S,"#ferror in function 'new'.",&tolua_err);
 return 0;
#endif
}

/* method: Get of class  A */
static int tolua_tClass_A_Get00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
 tolua_Error tolua_err;
 if (
 !tolua_isusertype(tolua_S,1,"A",0,&tolua_err) ||
 !tolua_isnoobj(tolua_S,2,&tolua_err)
 )
 goto tolua_lerror;
 else
#endif
 {
  A* self = (A*)  tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
 if (!self) tolua_error(tolua_S,"invalid 'self' in function 'Get'",NULL);
#endif
 {
  int tolua_ret = (int)  self->Get();
 tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
 }
 }
 return 1;
#ifndef TOLUA_RELEASE
 tolua_lerror:
 tolua_error(tolua_S,"#ferror in function 'Get'.",&tolua_err);
 return 0;
#endif
}

/* Open function */
TOLUA_API int tolua_tClass_open (lua_State* tolua_S)
{
 tolua_open(tolua_S);
 tolua_reg_types(tolua_S);
 tolua_module(tolua_S,NULL,0);
 tolua_beginmodule(tolua_S,NULL);
 tolua_cclass(tolua_S,"A","A","",NULL);
 tolua_beginmodule(tolua_S,"A");
 tolua_variable(tolua_S,"a",tolua_get_A_A_a,tolua_set_A_A_a);
 tolua_function(tolua_S,"new",tolua_tClass_A_new00);
 tolua_function(tolua_S,"Get",tolua_tClass_A_Get00);
 tolua_endmodule(tolua_S);
 tolua_endmodule(tolua_S);
 return 1;
}

如果生成成功就没有任何的提示.现在可以把tClassLua.cpp加到我们的工程中;
到最后的使用过程
注意:由于生成的过程中,根据包的文件生成的一个函数,这里我们需要在Main中定义一下!
TOLUA_API int tolua_tClass_open (lua_State* tolua_S);
然后需要调用它,让它把生成的代码内嵌到LUA中.

Main.cpp
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

#include "tolua.h"
#include "tClass.h"
#include <string>

TOLUA_API int tolua_tClass_open (lua_State* tolua_S);

int main ()
{
 lua_State* L = lua_open();
 luaopen_base(L);
 
 tolua_tClass_open( L );

// 调用脚本过程
 // 在脚本中就可以随意的使用这个类型
 std::string str = "obj = A:new(2); print(obj:Get() )";

lua_dostring( L,str.c_str() );

lua_close(L);

return 0;
}

tolua还有比较多的功能,都是能够将C\C++的类型,函数,结构等等,方便的内嵌入LUA中调用

toLua++使用(转)相关推荐

  1. 使用tolua++编译pkg,从而创建自定义类让Lua脚本使用

    2019独角兽企业重金招聘Python工程师标准>>> 在Lua第三篇中介绍了,如何在cocos2dx中使用Lua创建自定义类供Lua脚本调用使用,当时出于Himi对Lua研究不够深 ...

  2. U3D 扩展方法 Dotween tolua

    U3D 扩展方法 & Dotween & tolua using UnityEngine; using System.Collections; using LuaInterface;/ ...

  3. 关于tolua的使用

    一.首先在引擎的跟目录下找到cocos2d-x自带的工具tolua++ 二.使用tolua++生成自定义类的声明 打开tool文件夹中的readme文件如下: [cpp] view plaincopy ...

  4. tolua打包Android后路径出错,unity+tolua 64位android崩溃排查过程记录

    最近项目上线googleplay,需要打64位包,由于unity l2cpp打包比mono慢很多,因此前期开发过程中都是用的mono打的32位包进行测试.结果64位包出来后发生各种莫名其妙的卡死,尤其 ...

  5. Java和U3D比较,Unity热更方案 ILRuntime 和 toLua的比较

    前言 目前市面上流行的热更方案就是lua系列和ILRuntime,选取哪一种需要根据自己的项目进行比对. 无论是ILRuntime还是toLua都是市面上有在用到的热更方案.直观上来讲,都可以通过把代 ...

  6. Unity热更新学习(二) —— ToLua c#与lua的相互调用

    tolua 下载地址:http://www.ulua.org/index.html c#调用lua的方法,tolua的官方例子提供了很多种.我初步学了一种在做项目使用的方法.通过DoFile方法执行l ...

  7. tolua#是Unity静态绑定lua的一个解决方案

    tolua#是Unity静态绑定lua的一个解决方案 参考文章: (1)tolua#是Unity静态绑定lua的一个解决方案 (2)https://www.cnblogs.com/Leo_wl/p/6 ...

  8. Unity ToLua 中Update的调用流程

    1.首先注册Lua中的update函数 LuaState.cs 其中的OpenBaseLuaLibs() public void Start(){ #if UNITY_EDITORbeStart = ...

  9. Unity toLua加载lua的流程

    1.Unity加载Lua文件的流程 Unity中我们要加载Lua文件调用的函数是:LuaState类中的DoFile("xxx").我们可以看到流程是: LuaState:DoFi ...

  10. tolua++ 使用有感

    最近努力的完善基于 cocos2d-x lua 框架,要靠 tolua++ 来封装 hosts 用到的类,发现这东西还是不太智能,存在很多需要注意的地方. 如果要自己扩展数据类型,比如支持 lua 函 ...

最新文章

  1. 第二次作业+105032014001
  2. chrome items hidden by filters
  3. c 语言 多进程,VC++中进程与多进程管理的方法详解
  4. python 办公自动化-用python进行办公自动化都需要学习什么知识呢?
  5. linux 触摸屏在dev的那个目录下,各硬件设备在Linux中的文件名
  6. android编辑框显示,为EditText输入框加上提示信息
  7. 移动开发者大会.html5。Android。ios。wp联盟
  8. SQL Sever 2008 R2安装步骤
  9. Excel自定义格式技巧,案例解读(Excel入门Excel教程Excel函数)
  10. Mac 右键展示Copy path
  11. mysql 80070057_0x80070057错误原因
  12. pb中数据窗口函数小结(转)
  13. 2022-2027年中国农用机械融资租赁行业发展监测及投资战略咨询报告
  14. 分布式记账的几种方式
  15. 【DL】第 9 章:新兴的神经网络设计
  16. 华为数字化转型之道 实践篇 第八章 数字化交易:让做生意简单、高效
  17. 中国IT教育培训年度评选颁奖
  18. node JS獲取GPS_Python与Node.JS:哪一个最适合您的项目?
  19. 数据库管理工具DBeaver的下载以及安装
  20. element饿了么ui表格选中后高亮颜色修改

热门文章

  1. 微信支付以及接入SDK支付
  2. 51单片机入门——STC89C52RC控制步进电机进行转动、调速
  3. iOS-长按识别二维码/生成二维码/扫描二维码
  4. ~scanf()简析
  5. Android 之路20---Java基础14
  6. MBA-day1数学-应用题-利润问题
  7. 02 【版本控制命令】
  8. 7 - 6 复数类的定义
  9. 计算机信息系统集成一级和二级资质认证通过企业名单
  10. 计算机底层:CPU结构与组成原理、工作原理