C ++ calls Lua and lua

Source: Internet
Author: User

C ++ calls Lua and lua

Reprinted please indicate the source: http://blog.csdn.net/zhy_cheng/article/details/39756423


The cocos2d-x version I use is 2.3.3, first configuring the lua environment in a C ++ project.


First import the lua Project

1. The liblua project is the cocos2d-x-2.2.3 \ scripting \ lua \ proj. win32 \ liblua. vcxproj file, imported to the VS2010 Project

2. include Directory: add $ (ProjectDir) to the project properties-configure properties-C/C ++-General-Add the include directory ).. \.. \.. \ scripting \ lua \ tolua, $ (ProjectDir ).. \.. \.. \ scripting \ lua,

$ (ProjectDir)... \ scripting \ lua

3. Add liblua. lib and lua51.lib to properties-configure properties-linker-input-add Dependencies

Now, the lua environment is configured.

Add header file reference # include "CCLuaEngine. h", # include "script_support \ CCScriptSupport. h" to HelloWorld. cpp"

The lua code is as follows:

pageName="equip"a={student="zhangsan",age=23}function main()print("leoooooooooooooo")endfunction printSomething(name,age)print("-----------------name is "..name)print("-----------------age is "..age)endfunction returnSomething(name,age)return name,ageend

For the lua code written in this example, see the C ++ section. First, add it to AppDelegate. cpp.

CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
To set lua's Engine

Then load the lua code in the HelloWorld. cpp click event.

CCLuaEngine *pEngine = CCLuaEngine::defaultEngine();pEngine->executeString("require \"lua/hello.lua\"");

The following describes three knowledge points.

1. Call the lua Function

// Call the function lua_State * L = pEngine-> getLuaStack ()-> getLuaState (); // obtain the top stack, and save the int top = lua_gettop (L); lua_getglobal (L, "main"); // if (! Lua_isfunction (L,-1) {CCLog ("------------ return");} // The first parameter is the stack status pointer, and the second parameter is the number of parameters, the third parameter is the number of returned values. The fourth parameter is the address lua_pcall (L, 0) of the callback function with an error. // restore the stack lua_settop (L, top ); // call the function with parameters and no return value. top = lua_gettop (L); lua_getglobal (L, "printSomething"); // check if it is not there. if (! Lua_isfunction (L,-1) {CCLog ("------------ return");} // corresponds to lua_pushstring (L, "zhycheng"); lua_pushnumber (L, 24 ); lua_pcall (L, 2, 0, 0); lua_settop (L, top); // call a function with parameters that return values: top = lua_gettop (L); 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 is % d", age); CCLog ("name % s", name); lua_settop (L, top );

2. Obtain the value of a global variable of lua.

// Read lua's global variable top = lua_gettop (L); 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. Access a value of global table

// Obtain the valuetop = lua_gettop (L); lua_getglobal (L, "a"); if (! Lua_istable (L,-1) {CCLog ("error ----------------");} lua_pushstring (L, "student"); // lua_gettable is a function, it first let the key value exit, get the value of the corresponding table element, and then put the value into the stack // student's position in-2 at this time, and then the key is out of the stack, put 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); // be sure to pay attention to the stack issue when getting it again. // obtain the 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 );

Well, that's all. Stack operations can start from-1 or from 1, or start from-1, that is, from the top of the stack, starting from 1 is from the bottom of the stack.

Resources: http://download.csdn.net/detail/zhy_cheng/7999945


C. Call the functions in the lua file.

Which lua version are you using ????
I use lua 5.2, which is compiled under codeblock.
Your description does not match the source code.

Printf ("% d \ n", (int) lua_tonumber (m_pState, 1 ));

0 should be displayed.
In the lua Stack
Index 1 => add function
Index 2 => 1
Index 3 => 2
So the code tries to convert the lua function into a number. It should be 0.
The following sentence should get 3.
You said that your result is 2 0. It should not. Unless the code you post is different from the actually compiled code.

C and lua function call

Can I post your key code? Otherwise, you will not be able to know whether your usage is wrong or whether the function is not implemented due to some minor details.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.