分享好友 编程语言首页 频道列表

cocos2dx3.0rc导出自定义类到lua的方法 Cocos2d-x游戏开发 手把手教你Lua语言的编程方法

Lua  2023-02-09 08:090

以前要导出c++类到lua,就得手动维护pkg文件,那简直就是噩梦,3.0以后就会感觉生活很轻松了。

转载请注明出处http://www.cnblogs.com/mrblue/p/3637910.html

 

下面我就在说下具体做法。

 

1、安装必要的库和工具包,以及配置相关环境变量,请按照cocos2d-x-3.0rc0\tools\tolua\README.mdown说得去做,不做赘述。

2、写c++类(我测试用的是cocos2d-x-3.0rc0\tests\lua-empty-test\project\Classes\HelloWorldScene.cpp)

3、写一个生成的python脚本,你不会写,没关系,我们会照猫画虎

   1)进入目录cocos2d-x-3.0rc0\tools\tolua,复制一份genbindings.py,命名为genbindings_myclass.py

   2)把生成目录制定到咱工程里去,打开genbindings_myclass.py把

output_dir = '%s/cocos/scripting/lua-bindings/auto' % project_root

 改成

output_dir = '%s/tests/lua-empty-test/project/Classes/auto' % project_root

 3)修改命令参数,把

cmd_args = {'cocos2dx.ini' : ('cocos2d-x', 'lua_cocos2dx_auto'), \
                    'cocos2dx_extension.ini' : ('cocos2dx_extension', 'lua_cocos2dx_extension_auto'), \
                    'cocos2dx_ui.ini' : ('cocos2dx_ui', 'lua_cocos2dx_ui_auto'), \
                    'cocos2dx_studio.ini' : ('cocos2dx_studio', 'lua_cocos2dx_studio_auto'), \
                    'cocos2dx_spine.ini' : ('cocos2dx_spine', 'lua_cocos2dx_spine_auto'), \
                    'cocos2dx_physics.ini' : ('cocos2dx_physics', 'lua_cocos2dx_physics_auto'), \
                    }

  改成

cmd_args = {'myclass.ini' : ('myclass', 'lua_myclass_auto') }

 4)这时你可能问myclass.ini在哪啊,我们下来就写这个文件。原理一样,我还是照猫画虎,拿cocos2dx_spine.ini改的。

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

# 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
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/cocos/2d -I%(cocosdir)s/cocos/base -I%(cocosdir)s/cocos/ui -I%(cocosdir)s/cocos/physics -I%(cocosdir)s/cocos/2d/platform -I%(cocosdir)s/cocos/2d/platform/android -I%(cocosdir)s/cocos/math/kazmath -I%(cocosdir)s/extensions -I%(cocosdir)s/external -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s

cocos_flags = -DANDROID -DCOCOS2D_JAVASCRIPT

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/tests/lua-empty-test/project/Classes/HelloWorldScene.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 = HelloWorld

# what should we skip? in the format ClassName::[function function]
# 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 = Ref ProcessBase

# 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

  

  改的时候要注意这些行

[myclass]
prefix = myclass
target_namespace =
headers = %(cocosdir)s/tests/lua-empty-test/project/Classes/HelloWorldScene.h
classes = HelloWorld
skip = 
abstract_classes = 

4、下面要自动生成代码了,打开命令行工具,cd到cocos2d-x-3.0rc0\tools\tolua下,敲入python genbindings_myclass.py

回车运行。如果前面没问题的话你会在cocos2d-x-3.0rc0\tests\lua-empty-test\project\Classes多了一个文件夹auto,然后把里面生成lua_myclass_auto.cpp和lua_myclass_auto.hpp加入拽如工程

5、把我们生成的个module在脚本引擎初始化的时候加入lua。

编辑AppDelegate.cpp,包含lua_myclass_auto.hpp头文件,在

LuaEngine* engine = LuaEngine::getInstance();

后面加入

register_all_myclass(engine->getLuaStack()->getLuaState());

 

6、编译运行。这样HelloWorld这个类就被导出到lua了。

 

测试------------------------------------------------

打开hello.lua,编辑local function main()这个函数

把前面改成

local function main()
    -- avoid memory leak
    collectgarbage("setpause", 100)
    collectgarbage("setstepmul", 5000)

    local hello = HelloWorld:create()
    local sceneGame = cc.Scene:create()
    sceneGame:addChild(hello)
    cc.Director:getInstance():runWithScene(sceneGame)

    if(1==1) then
        return
    end

……
……

 

运行(注意如果代码没变动是不是拷贝脚本资源的,要手动拷贝),你就会看到HelloWorld,而不是小松鼠了。

查看更多关于【Lua】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
LUA解析json小demo
需要修改的json数据gui-config.json{"configs": [{"server": "JP3.ISS.TF","server_port": 443,"password": "58603228","method": "aes-256-cfb","remarks": ""},{"serv

0评论2023-03-16958

第二十三篇:在SOUI中使用LUA脚本开发界面
像写网页一样做客户端界面可能是很多客户端开发的理想。做好一个可以实现和用户交互的动态网页应该包含两个部分:使用html做网页的布局,使用脚本如vbscript,javascript做用户交互的逻辑。当需求变化时,只需要在服务端把相关代码调整一下,用户即可看到新的

0评论2023-03-16307

windows下编译lua源码"><转>windows下编译lua源码
因为之前一直使用 lua for windows 来搭建lua的使用环境,但是最新的 lua for windows 还没有lua5.2,我又想用这个版本的lua,所以被逼无奈只能自己编一下lua源码。首先从 lua的官网 下载你想要使用的lua源码,比如我下载的就是lua5.2。解压后内容如下:

0评论2023-03-16723

lua:使用Lua处理游戏数据
在之前lua学习:lua作配置文件里,我们学会了用lua作配置文件。其实lua在游戏开发中可以作为一个强大的保存、载入游戏数据的工具。 比如说,现在我有一份表单:data.xls用什么工具解析这个Excel文件并将数据载入游戏?我们可以使用Lua来完成这个工作。不过要

0评论2023-03-16955

cocos2d-lua 控制台输入Lua指令方便调试
用脚本进行开发,如果不能实时去输入指令,就丧失了脚本的一大特色,所以对cocos2d-x程序稍微修改下,使其可以直接从控制台读入lua指令,方便调试。1 首先在行首加入lua的引用,如下1 #include "main.h"2 #include "AppDelegate.h"3 #include "cocos2d.h"4 #i

0评论2023-02-09995

lua_touserdata
void *lua_touserdata(lua_State*L,intindex);如果给定索引处的值是一个完整的userdata,函数返回内存块的地址。如果值是一个lightuserdata,那么就返回它表示的指针。否则,返回NULL。例如: 在CCLuaStack::executeFunction()函数中有一段代码是用来获取c++

0评论2023-02-09613

Lua 5.2 中文参考手册
闲来无事,发现Lua更新到了5.2.2,参考手册也更到了5.2,在网上发现只有云风翻译的5.1版,花了几天时间翻译了一些。参考手册有点长,又要随时修改,所以在github上建了项目,有需要的朋友可以看看,同时也欢迎指正。中文手册:Lua 5.2中文参考手册

0评论2023-02-09578

lua报错,看到报错信息有tail call,以为和尾调用有关,于是查了一下相关知识
  尾调用是指在函数return时直接将被调函数的返回值作为调用函数的返回值返回,尾调用在很多语言中都可以被编译器优化, 基本都是直接复用旧的执行栈, 不用再创建新的栈帧, 原理上其实也很简单, 因为尾调用在本质上看的话,是整个子过程调用的最后执行语句,

0评论2023-02-09333

lua 实现tableToString
function tableToString(studentNum) local str ="{ " str = str.."\n" for k, v in pairs(studentNum) doif type(v) ~= "table" thenstr = str.."[\""..k.."\"]"str = str..":"str = str..vstr = st

0评论2023-02-09824

Lua类对象和类对象的单例 lua实例
1、Lua的类对象local myClass = {}function myClass:new()local self = {}setmetatable(self,{__index = myClass})endlocal a = 0local b = 0local c = 0return myClass以上类的对象实例化的调用:require "myClass"local _myClass = myClass:new()实例化后 _

0评论2023-02-09653

浅析一个lua文件窥slua工作机制 lua 调用so
slua的东西不是几句话能讲得完,这里只说结论不说原因,原因有空写个Little Slua工程来解释,下面注释中有几个关键点:LuaVar系列类:LuaFunction,LuaTable,LuaDelegate的使用,类型表和实例表,__parent代表继承关系,存ud的表是弱表(可以用来缓存c#中引用

0评论2023-02-09602

lua学习笔记10:lua简单的命令行 lua怎么执行
前面反复使用的命令行,好学喜欢命令行:一 格公式lua [options][script][args]两 详细命令-e 直接命令传递一个lua-l 加载文件-i 进入交互模式比例如。端子输入:lua -e "print(math.sin(12))" 版权声明:本文博主原创文章,博客,未经同意不得转载。

0评论2023-02-09533

Lua中强大的元方法__index详解 lua元表和元方法
今天要来介绍比较好玩的内容——__index元方法1.我是备胎,记得回头看看咳咳,相信每一位女生都拥有或者不知不觉中拥有了一些备胎,啊,当然,又或许是成为过别人的备胎。没有备胎的人,就不是完整的人生。(小若:停!) 我们来想象一下,如果对一个table进

0评论2023-02-09494

Lua调试工具使用及原理 lua运行原理
前言当我们在linux下使用c/c++开发时,可以通过gdb来调试我们编译后的elf文件。gdb支持了attch、单步运行(单行、单指令)、设置断点等非常实用的功能来辅助我们调试。当使用lua开发的时候,一般可能会使用print(打印到屏幕)或是输出日志等稍微简陋的调试方

0评论2023-02-09999

(转)lua protobuffer的实现
转自: http://www.voidcn.com/article/p-vmuovdgn-bam.html (1)lua实现protobuf的简介需要读者对google的protobuf有一定的了解。Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but s

0评论2023-02-09818

lua require路径设置实例
1.首先要强调的是,lua require的路径用的是斜杠"/",而不是从Windows文件属性那里复制来的反斜杠"\"。2.通过 print(pagckage.path) 和print(package.cpath)打印lua系统封装的两个全局属性可以看到当前lua解析器require的时候默认替换的路径3.更改路径的时候

0评论2023-02-09855

更多推荐