Call C + + function instance _lua in Lua

Source: Internet
Author: User
Tags lua

So far, there should be some familiarity with the communication between LUA and C + +, and today we'll show you the last one.
(Narrator: What?) The last one? Is it over yet? That's great, ~!. )

Previous Chapter Portal: http://www.jb51.net/article/55097.htm

1. Lua calls C + + functions

Lua to invoke C + + functions is quite convenient, first of all, we create a C + + function first:

Copy Code code as follows:

Public
static int getnumber (int num);
  
  
int hellolua::getnumber (int num) {
Cclog ("GetNumber num =%d", num);
return num + 1;
}

Class or something, I just ignored that directly on the function.
(Narrator: = = good lazy!) )

This is a very simple function that gives a value that returns a value of +1.
Now, we want to call this function in Lua, we need to write one more function.
(Narrator: That, why the GetNumber function is static?) )
Copy Code code as follows:

Public
static int Cpp_getnumber (lua_state* PL);


int Hellolua::cpp_getnumber (lua_state* pL) {
/* Take a value from the top of the stack * *
int num = (int) lua_tonumber (PL, 1);

/* Call the GetNumber function, the return value into the stack * *
Lua_pushnumber (PL, GetNumber (num));

/* Returns the number of values, GetNumber only one return value, so return 1 * * *
return 1;
}


(Narrator: It's static again!) )
What the hell is going on here? We know that Lua and C + + can only communicate through the stack, so it's impossible for Lua to call the GetNumber function directly, so we set up a cpp_getnumber function as an intermediary.
Cpp_getnumber function has a lua_state* pl parameter, with this parameter, C + + will be able to take value from the LUA stack, the rest is very simple, do not want too much explanation ...

Let's just explain.
(Narrator: Your sister paper, I know!) )

1 First, the LUA script will call the Cpp_getnumber function, and as for why it can be invoked, once explained, I know that the narrator will definitely ask. (Narrator: A feeling of inability to spit ... )
2 when Cpp_getnumber is called, everything goes back to the operation of Lua in C + +, the top of the stack will store the parameters required by the function, take out to use on it.
3 after LUA calls Cpp_getnumber, you need a result, of course, the result can only be stored in the stack, so it is logical to put the results of getnumber into the stack.
4 Finally, Cpp_getnumber returns a value that is not the result of the function's execution, but rather the number of GetNumber needs to return (LUA supports multiple return values)
Narrator: Did you get it?
(Narrator: The amount ...). Don't understand)

Well, since everyone understands, I won't say more.
(Narrator: Your Sister paper's o O!) )

2. The end of

Finally, the tutorial on Lua came to an end, and I wanted to write a luabind tutorial (very powerful!). )。 However, writing a tutorial is a time-consuming task, 10 minutes to learn the knowledge, maybe I will take 2 hours to birth a tutorial. (Narrator: OK, so let's support him more ...)
 
However, it is not much of a challenge to take these 6 tutorials thoroughly and then develop them in cocos2d-x with Lua. (Better get to know Luabind)
Maybe, I'm going to go on to write the Lua tutorial, and now I'm going to continue to study cocos2d-x. Because of the tutorial (which has been writing the state machine tutorial and the LUA tutorial for two weeks), I've stopped for nearly two weeks. Must hurry up ~ cannot lag behind ~
So, I wish you a happy New Year's day.
(Narrator: Are you sure you will post this tutorial after New Year's Day?)
 
Then, I hope that everyone gently moving the mouse, click on the "top" ~ hehe

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.