YuLei Liao Call LUA local function from Cocos2D-x :)
Posts 59
Added by YuLei Liao over 1 year ago

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 ?

YuLei Liao RE: How to call LUA local function from Cocos2D-X?
Posts 59
Added by YuLei Liao over 1 year ago

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)
YuLei Liao RE: How to call LUA local function from Cocos2D-X?
Posts 59
Added by YuLei Liao over 1 year ago

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

alex cheng RE: Call LUA local function from Cocos2D-x :)
Posts 15
Added by alex cheng over 1 year ago

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

YuLei Liao RE: Call LUA local function from Cocos2D-x :)
Posts 59
Added by YuLei Liao over 1 year ago

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.

alex cheng RE: Call LUA local function from Cocos2D-x :)
Posts 15
Added by alex cheng over 1 year ago

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?

Eric Tbone RE: Call LUA local function from Cocos2D-x :)
Posts 1
Added by Eric Tbone over 1 year ago

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.

qi luo RE: Call LUA local function from Cocos2D-x :)
Posts 4
Added by qi luo over 1 year ago

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!

test.lua (7.6 kB)

test.lua (7.6 kB)


(1-7/7)