Lua and C + + sixth (LUA calls C + + functions)

Source: Internet
Author: User
Tags lua

This article was reproduced in : http://blog.csdn.net/musicvs/article/details/8451369

Stupid wood to contribute, what? Player? No, it's the heart.

Body:

1. Lua calls C + + functions

It's pretty handy for Lua to call C + + functions, first, let's create a C + + function first:

 Public :   Static int getnumber (int  num);       int int num) {      Cclog ("getnumber num =%d", num);       return 1 ;  }  

Class of what, I just ignored, directly on the function.

(Narrator: = = good lazy!) )

This is a very simple function that gives a value that returns a value after +1.

Now, we want to call this function in Lua and write a function more.

(Narrator: Well, why is the GetNumber function static?) )

 Public:  Static intCpp_getnumber (lua_state*PL); intHellolua::cpp_getnumber (lua_state*PL) {      /*take a value from the top of the stack*/      intnum = (int) Lua_tonumber (PL,-1); /*Call the GetNumber function to return the value into the stack*/Lua_pushnumber (PL, GetNumber (num)); /*Returns the number of values, GetNumber only one return value, so returns 1*/      return 1; }

(Narrator: It's static again!) )

What's going on here? We know that Lua and C + + can only communicate through the stack, so it is not possible for LUA to call the GetNumber function directly, so we create a cpp_getnumber function as an intermediary.

Cpp_getnumber function has a lua_state* pl parameter, with this parameter, C + + can be taken from the LUA stack, the rest is very simple, do not want to explain too much ...

Let's just explain it.

(Narrator: Your sister's paper, I know!) )

1) First, the Lua script will call the Cpp_getnumber function, as for why it can be called, a reply, I know the narrator will ask. (Narrator: A feeling of inability to spit out the groove ...) )

2) When Cpp_getnumber is called, everything goes back to the C + + operation of Lua, and the stack top will store the parameters needed for the function, which can be taken out.

3) After Lua calls Cpp_getnumber, a result is required, and of course, the result can be stored in the stack, so it's natural to put the results of getnumber into the stack.

4) Finally, Cpp_getnumber returns a value that is not the result of the function execution, but rather the number of getnumber required to return the value (LUA supports multiple return values of the function)

Narrator, do you understand?

(Narrator: The amount ... Not understand)

Well, since we all understand, I will not say more ~

(Narrator: o O of your sister's paper!) )

-------------------------------------------supplement to begin-------------------------from 2014.10.15

I'm terribly sorry. I missed something very important: How does Lua invoke C + + functions?

See someone comment today, and then look again tonight, only to find out, really leaked!

Add the following:

Now, to get started with these two functions, modify the Hellolua init function:

BOOL Hellolua::init () {      lua_state* PL = lua_open ();      Luaopen_base (PL);         /*  **      "cpp_getnumber", Cpp_getnumber)      ; " Hellolua.lua " );      Lua_close (PL);       return true ;  }

Finally, you will also modify the Hellolua.lua script file:

--Hellolua.lua file

Local num = Cpp_getnumber (10);

To run the project in debug mode, we will see the following log output:

GetNumber num = 10

On behalf of the GetNumber function was called, in other words, Cpp_getnumber was successfully called.

Now to explain the most important point, why in Lua can invoke the Cpp_getnumber function, LUA can lua_register C + + static functions into Lua, so that LUA can directly invoke C + + functions.

As to why the Cpp_getnumber function must be static, it is simple, if it is not a static function, it has to be created before the object can be called. In Lua, it is not possible to create a Hellolua object again (of course, in Lua it is possible to create a C + + object, which is not discussed for the moment), so the registered function must be static.

---------------------------------------the supplemental End from 2014.10.15-------------------------

2. It's over.

Finally, the tutorial on Lua, to this end, originally I would like to write Luabind tutorial (very powerful!). )。 However, writing a tutorial is a very time-consuming work, 10 minutes to learn the knowledge, maybe I will take 2 hours to birth a tutorial. (Narrator: OK, so let's have more support for him ...) )

However, if you thoroughly understand the 6 tutorials and then use LUA to develop it in cocos2d-x, there's no big difficulty. (It's best to get to know Luabind again)

Maybe I'll go on to write the Lua tutorial later, and now I'm going to continue to learn cocos2d-x-because of the tutorial (which has been writing the state machine for two weeks and the Lua tutorial), I stopped for nearly two weeks. Gotta hold on ~ can't fall behind ~

Well, I wish you a happy new year.

(Narrator: Are you sure you will post this tutorial after New Year's Day?) )

Then, I hope you gently moving the mouse, click on the "top" ~ hehe

.

By the way, forget a very important thing, all the source code of this series of tutorials:

http://download.csdn.net/detail/musicvs/4948713

Lua and C + + sixth (LUA calls C + + functions)

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.