【教程分享】Cocos如何绑定lua自定义类

从论坛和技术支持的反馈来看,很多童鞋在Cocos中进行lua绑定时遇到了不少问题,因为Cocos使用的是预编译库的方式,所以和之前用Cocos2d-x绑定又不太一样。原有的教程也需要做调整,这篇文章目的是在Cocos中创建一个自定义类,并进行lua绑定,然后在lua中调用C++。

关于如何在Cocos2d-x中进行lua绑定自定义类,请看http://cocos2d-x.org/wiki/Binding_Custom_Class_To_Lua_Runtime。本文是基于此教程做的调整。

1.版本说明

环境: Mac OSX

Cocos: v2.1

Cocos2d-x: v3.4Final

Cocos Code IDE: v1.2.0

Cocos Studio: v2.1.2 beta(这里没用到)

2.在Cocos中进行lua绑定自定义类

2.1 创建项目

首先,创建一个Cocos项目,名字为CocosLuaBinding。

2.2 发布项目

创建后默认会使用Cocos Studio打开,选择发布为XCode工程。

2.3 添加自定义类

在XCode中,项目的Classes目录下添加Custom类。


// CustomClass.h

#ifndef __CUSTOM__CLASS
#define __CUSTOM__CLASS
#include "cocos2d.h"

namespace cocos2d {
class CustomClass : public cocos2d::Ref
{
public:
    CustomClass();
    ~CustomClass();
    bool init();
    std::string helloMsg();
    CREATE_FUNC(CustomClass);
};
} //namespace cocos2d

#endif // __CUSTOM__CLASS


// CustomClass.cpp
#include "CustomClass.h"
USING_NS_CC;

CustomClass::CustomClass(){
}

CustomClass::~CustomClass(){
}

bool CustomClass::init(){
    return true;
}

std::string CustomClass::helloMsg() {
    return "Hello from CustomClass::sayHello";
}

2.4 添加cocos2dx_custom.ini文件

由于使用的是Cocos Framework,所以创建的cocos2dx_custom.ini路径是 /Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua/(默认安装路径)。需要注意的是headers的路径要指向项目/frameworks/runtime-src/Classes/,这里我用了绝对路径,您需要修改为自己的路径。


# the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = cocos2dx_custom

# create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`)
# all classes will be embedded in that namespace
target_namespace =

android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/include
android_flags = -D_SIZE_T_DEFINED_ 

clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include 
clang_flags = -nostdinc -x c++ -std=c++11

cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/my -I%(cocosdir)s/cocos/base -I%(cocosdir)s/cocos/platform/android
cocos_flags = -DANDROID

cxxgenerator_headers = 

# extra arguments for clang
extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s 

# what headers to parse
#headers = %(cocosdir)s/cocos/my/CustomClass.h
headers = /Users/Jacky/Workspace/CocosLuaBinding/frameworks/runtime-src/Classes/CustomClass.h

# what classes to produce code for. You can use regular expressions here. When testing the regular
# expression, it will be enclosed in "^$", like this: "^Menu*$".
classes = CustomClass.*

# what should we skip? in the format ClassName::
# ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also
# regular expressions, they will not be surrounded by "^$". If you want to skip a whole class, just
# add a single "*" as functions. See bellow for several examples. A special class name is "*", which
# will apply to all class names. This is a convenience wildcard to be able to skip similar named
# functions from all classes.

skip = 

rename_functions = 

rename_classes = 

# for all class names, should we remove something when registering in the target VM?
remove_prefix = 

# classes for which there will be no "parent" lookup
classes_have_no_parents = 

# base classes which will be skipped when their sub-classes found them.
base_classes_to_skip = 

# classes that create no constructor
# Set is special and we will use a hand-written constructor
abstract_classes = 

# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.
script_control_cpp = no

2.5 修改genbindings.py

genbindings.py脚本是用来运行绑定的脚本。这里的路径是/Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua/genbindings.py。

找到cmd_args,加入下面这行:

  'cocos2dx_custom.ini' : ('cocos2dx_custom', 'lua_cocos2dx_custom'), \

output_dir参数指定了绑定文件的输出目录,这里可以更改为Classes,也可以不改。这里就不改了。

cmd_args的其他行是引擎的脚本绑定,如果未做修改,可以删除以避免引擎文件的重复绑定,加快绑定速度。这里也不改了。

2.6 运行绑定脚本

打开终端,进入tolua目录,运行刚才修改的genbindings.py脚本。

cd /Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua
./genbindings.py

这里不讨论绑定失败的情况,大部分是环境变量没配好的原因,在运行之前请先阅读/Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua/README.mdown。

2.7 拷贝绑定文件到项目目录

在成功运行完绑定脚本后,绑定文件会生成在/Applications/Cocos/frameworks/cocos2d-x-3.4/cocos/scripting/lua-bindings/auto/lua_cocos2dx_custom.cpp(h)

将这俩个文件剪切到Classes目录,并添加到XCode项目中。

到此为止,基本上是和Binding_Custom_Class_To_Lua_Runtime一致的。

3.编译运行

由于使用的是Cocos Framework,所以引擎没有源码了,只有预编译库,这也就是为什么在第二步中我们主要做的调整:把自定义类及其绑定文件放到Classes中。

3.1 注册自定义类

打开Classes/lua_module_register.h文件,添加头文件

include "lua_cocos2dx_custom.hpp"

在static int lua_module_register(lua_State* L)添加注册函数

register_all_cocos2dx_custom(L);

3.2 关闭COCOS_IDE_DEBUG_SUPPORT

打开Classes/ide-support/CodeIDESupport.h文件,修改

define CC_CODE_IDE_DEBUG_SUPPORT 0

3.3 在Cocos IDE中构建自定义模拟器

在Cocos IDE中打开项目,选中CocosLuaBinding项目,右键,Cocos工具,构建自定义模拟器,选择相应的模拟器,点击生成。

因为Cocos Framework默认使用自带的模拟器,所以如果修改了C++文件,仍然使用默认模拟器,修改的C++文件不会生效。

构建自定义模拟器后,以后每次都会使用自定义的模拟器。

3.4 如何知道目前使用的是自定义模拟器还是默认模拟器?

选中CocosLuaBinding项目,右键,调试方式,调试配置。

以Mac平台为例:

自定义模拟器路径为/Users/Jacky/Workspace/CocosLuaBinding/runtime/mac/CocosLuaBinding Mac

默认模拟器路径为/Applications/Cocos/cocos-simulator-bin/mac/Simulator.app

4.测试绑定是否成功

在src/app/views/MainScene.lua的function MainScene:onCreate()中添加如下测试代码

local customClass = CustomClass:create()
    local msg = customClass:helloMsg()
    release_print("customClass's msg is : " .. msg) --注意已经没有cclog了

5.搞定,收工~

3.2 关闭COCOS_IDE_DEBUG_SUPPORT
这么做之后,是不是无法在IDE中调试了?

能不能发一个 参数里带回调函数的例子?

问一下 构建自定义模拟器后不能输出日志时怎么个情况???

请教,我用COCOS2D-X 3.7.1,在WIN32下构建,完全按楼主的步骤做的,连测试的类都一样叫做CustomClass,内容也完全一样,但是始终卡在运行genbindings.py这里,错误提示如下:

File “…/generator.py”, line 1427 in main()
File “…/generator.py”, line 1398 in main
‘prefix’ : config.get(s, ‘prefix’),

File “…\python27\lib\ConfigParser.py”, line 618 in get
raise NoOptionError(option,section)

NoOptionError: No option ‘prefix’ in section: ‘cocos2dx_custom’

Generating lua bingdings fails.

但是我的cocos2dx_custom.ini文件完全是复制楼主的(header地址改为自己的地址,绝对地址和相对地址都测试过,同样问题),不应该不存在prefix的选项啊!

请教楼主,是不是新的COCOS2D-X版本又做了什么修改,或者我又什么地方没有做对?