Forum > Lua Binding > Call LUA local function from Cocos2D-x :)

Call LUA local function from Cocos2D-x :)

By dualface Posted 2011-09-30 22:09 Comments 7
dualface
  • Posts: 61

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 ?

dualface
  • Posts: 61

#1 RE: 2011-10-01 23:19

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)
dualface
  • Posts: 61

#2 RE: 2011-10-01 23:35

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

alexcheng
  • Posts: 15

#3 RE: 2011-10-03 21:14

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

dualface
  • Posts: 61

#4 RE: 2011-10-04 19:53

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.

alexcheng
  • Posts: 15

#5 RE: 2011-10-05 17:55

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?

Mud
  • Posts: 1

#6 RE: 2012-01-19 22:49

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.

yiqideren
  • Posts: 4

#7 RE: 2012-01-20 01:45

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

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

test.lua (7.6 kB)

test.lua (7.6 kB)

Loggin to reply

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps