Reprint Please specify source: http://blog.csdn.net/zhy_cheng/article/details/39756423
The cocos2d-x version I'm using is 2.3.3, which first configures the LUA environment in a C + + project.
First import the LUA project
1.liblua Project is cocos2d-x-2.2.3\scripting\lua\proj.win32\liblua.vcxproj this file, into the VS2010 project
2. Include directory: In the properties of the project-Configuration Properties-c/c++-General-add $ (ProjectDir) to the additional include directory. \.. \.. \scripting\lua\tolua,$ (ProjectDir): \.. \.. \scripting\lua\lua,
$ (ProjectDir): \.. \.. \scripting\lua\lua
3. In Properties-Configuration Properties-linker-Input-Additional dependencies join Liblua.lib and Lua51.lib
Well, the LUA environment is configured.
Add a header file to the HelloWorld.cpp reference # include "CCLuaEngine.h", #include "script_support\ccscriptsupport.h"
This is where Lua begins to write, and the LUA code is
Pagename= "Equip" a={student= "Zhangsan", age=23}function Main () print ("Leoooooooooooooo") endfunction printsomething ( name,age) Print ("-----------------name is": Name) print ("-----------------is"). Age) Endfunction returnsomething (name,age) return name,ageend
This written LUA code, see the C + + section below, first add in AppDelegate.cpp
ccluaengine* pengine = ccluaengine::d efaultengine (); Ccscriptenginemanager::sharedmanager ()->setscriptengine (pengine);
to set up the LUA engine
The LUA code is then loaded in the HelloWorld.cpp click event
Ccluaengine *pengine = ccluaengine::d efaultengine ();p engine->executestring ("Require \" Lua/hello.lua\ "");
The following is mainly about 3 points of knowledge
1. Functions that call Lua
Call the function with no parameter no return value lua_state* l=pengine->getluastack ()->getluastate ();//Get the top of the stack and save the value int top=lua_gettop (L); Lua_ Getglobal (L, "main");//See if it is not there if (!lua_isfunction (l,-1)) {Cclog ("------------return");} The first parameter is a pointer to the state of the stack, the second parameter is the number of arguments, the third parameter is the number of return values, the fourth parameter is the address of the callback function with the error lua_pcall (l,0,0,0);//restore Stack lua_settop (l,top);//Call parameters, function with no return value top=lua_gettop (l); Lua_getglobal (L, "printsomething");//See if it is not there if (!lua_isfunction (l,-1)) {Cclog ("--------- ---return ");} One by one corresponds to Lua_pushstring (L, "Zhycheng"); Lua_pushnumber (l,24); Lua_pcall (l,2,0,0); Lua_settop (l,top);//call has parameters, function Top=lua_gettop (l) with two return values, Lua_getglobal (L, "returnsomething"), if (!lua_isfunction (l,-1)) {Cclog ("------------ Return "); Lua_pushstring (L, "new"); Lua_pushnumber (l,22); Lua_pcall (l,2,2,0); if (!lua_isnumber (l,-1) | |! Lua_isstring (l,-2)) {Cclog ("return error");} Name in the following int age = (int) lua_tonumber (l,-1); const char* name=lua_tostring (L,-2); Cclog ("Age was%d", age); Cclog ("name%s", name); Lua_settop (l,top);
2. Get the value of a global variable for LUA
Read the global variable top=lua_gettop (l) of Lua, Lua_getglobal (L, "pageName"), if (!lua_isstring (l,-1)) {Cclog ("return error");} Const char* equipname=lua_tostring (L,-1); Cclog ("Name is%s", equipname); Lua_settop (l,top);
3. Accessing a value of a global table
Gets the Valuetop=lua_gettop (l) of a key in the Lua table, Lua_getglobal (L, "a"), if (!lua_istable (l,-1)) {Cclog (" Error----------------");} Lua_pushstring (L, "student");//lua_gettable is a function that first lets the key value out of the station, gets the value of the corresponding table element, and then puts the value in the stack//at this time student in the 2 seat, but this key out of the stack, Let his value into the stack lua_gettable (l,-2), if (!lua_isstring (l,-1)) {Cclog ("error--------------");} Const char* Studentname = lua_tostring (L,-1); Cclog ("Studentname is%s", studentname); Lua_settop (l,top);//When acquiring, be sure to pay attention to the problem of stack//Get Age Top=lua_gettop (L); Lua_getglobal ( L, "a"), if (!lua_istable (l,-1)) {Cclog ("error----------------");} Lua_pushstring (L, "age"); lua_gettable (l,-2); if (!lua_isnumber (l,-1)) {Cclog ("error-----------------");} int Aage=lua_tonumber (L,-1); Cclog ("Aage is%d", aage); Lua_settop (l,top);
OK, so much so, for the operation of the stack, can start from 1, can also start from 1, starting from 1, that is, from the top of the stack, starting from 1 is from the bottom of the stack upward.
Download resource: http://download.csdn.net/detail/zhy_cheng/7999945
C + + calls Lua