Always wanted to learn how to use the tolua++ tool in COCOS2DX to make LUA scripts call C + + functions, and to do it today, by the way:
First, we open the test vs project under projects in cocos2dx-2.2.4, and we can see that there is already a HelloWorld class in this example, and we use it to illustrate.
We then write the pkg file according to the definition of the HelloWorld class:
// myclass.pkg class Public cocos2d::cclayer{ virtualbool init (); static cocos2d::ccscene* scene (); void menuclosecallback (ccobject* psender); Static helloworld* Create ();};
We open the Readme file to see the syntax for writing a pkg file:
1. Generating the lua<-->c bindings with tolua++
Build scripts for Windows (BUILD.bat) and Unix (build.sh) is provided
To generate the relevant files after modifying the. pkg files. These
Scripts basically run the following command:
Tolua++.exe-l Basic.lua-o LuaCocos2d.cpp cocos2d.pkg
This would generate the bindings file and patch it with come cocos2dx
Specific modifications.
On POSIX systems can also just run ' make ' to build the bindings
If/when you change. pkg files.
2. Writing. pkg files
1) enum keeps the same
2) Remove Cc_dll for the class defines, pay attention to multi inherites
3) Remove inline keyword for declaration and implementation
4) Remove public protect and private
5) Remove the decalration of class member variable
6) Keep Static keyword
7) Remove Memeber functions that declared as private or protected
One thing to note is the static cocos2d::ccscene* scene (); This sentence is best written as a static ccscene* scene (), otherwise the following error will occur:
Error in function ' Runwithscene '.
Argument #2 is ' cocos2d::ccscene '; ' Ccscene ' expected.
I'm not sure why, just replace it with the wrong hint.
Then, we put the well-written pkg file under tolua++, click Run BUILD.bat, this file is mainly cocos2d.pkg contained in the PKG file integration generated LuaCocos2d.cpp, look at the internal syntax to know:
tolua++-L Basic.lua-o ". /.. /scripting/lua/cocos2dx_support/luacocos2d.cpp "cocos2d.pkg
We then introduced our HelloWorld header file in LuaCocos2d.h, adding the Lualib project to the project, adding a reference to the LUA header file in the project, adding Lua51.lib and Lualib.lib in the link.
Next we add a LUA script Helloworld.lua, which reads as follows:
Print ("") Local director = ccdirector:shareddirector ()local scene = Helloworld:scene () Director: Runwithscene (Scene)print("")
In AppDelegate.cpp introduction of the CCLuaEngine.h header file, the code is slightly modified as follows:
// Ccscene *pscene = Helloworld::scene (); // Pdirector->runwithscene (pscene); ccscriptengineprotocol* pengine = ccluaengine::d efaultengine (); Ccscriptenginemanager::sharedmanager (),setscriptengine (pengine); string fullpath = Ccfileutils::sharedfileutils ()->fullpathforfilename ("Helloworld.lua "); Ccluaengine::d efaultengine ()->executescriptfile (Fullpath.c_str ());
Last run look at:
Cocos2dx using tolua++ to make Lua invoke C + + functions