Call LUA local function from Cocos2D-x :)

Example code:

function tick(dt)
    ....
end

cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc("tick", 0.01, false)

In above code, has one problem: tick is GLOBAL FUNCTION.

Q: How to call LUA local function from Cocos2D-X ?

Ok, i fixed all problem.

I changed many cocos2d-x source files. Call lua local function from c++ it’s ready.

example code:

local function tick(dt)
    ....
end

cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc("tick", 0.01, false)

-----

local function btnTouchMove(e)
    ....
end

layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN, btnTouchBegin)

All changes based cocos2d-1.0.1-x-0.9.1.

git repo: https://github.com/dualface/cocos2d-x/tree/cocos2d-1.0.1-x-0.9.1-lua-fix

I use another way to implement it, it needn’t modify any cocos2d-x codes.
Please see http://blog.csdn.net/dragoncheng/article/details/6843638

alex cheng wrote:

I use another way to implement it, it needn’t modify any cocos2d-x codes.
Please see http://blog.csdn.net/dragoncheng/article/details/6843638

Very good!
But dispatching event through lua, performance maybe is problem.

maybe.
There are two ways to optimize:
1: hand the handle the call function after runing
2: The mapper was wrote by c code

BTW:Do we really care about what this performance?

Why were callbacks done by variable name anyway? Functions are first class values in Lua; you should be able to pass a function, not a string, and have that function called. The scope of the variable where that function value is stored in the users code should be irrelevant. You should be able to use anonymous functions if you want, too:

CCTouchDelegate:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN, function(e) end)

I can’t see YuLei Liao’s code (link is busted), but I imagine he just added support for storing function references.

If I add this to my copy of coco2d-x, how would I go about submitting it back to the repository? I’ve never contributed to an open source project before, but I know Lua quite well and will probably be working on this binding for my own use anyway.

this is my way,it needn’t modify any cocos2d-x codes. _

just replace the content of hello.lua in project HelloLua. Have a try!