Lua and C + + fifth (c + + calls LUA functions)

Source: Internet
Author: User

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

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

Body:

After the introduction of the previous chapters, I believe you are familiar with the stack of LUA, if you are not very familiar with the friend, it is recommended to look at the previous tutorial, or more than a few times the code.

So, if you're already familiar with the LUA stack, the next thing is easy.

Today, let's look at how C + + calls Lua's functions, first to see what the Lua file looks like now:

-- Hellolua.lua  file "beauty girl    " "Mutou "  the }    function Helloadd (NUM1, num2)      return (NUM1 + num2)  


We see a helloadd function, so now we're going to call this function in C + +.

(Narrator: must also use the Getglobal, every time has it ~!) = =)

Directly on the code:

/*C + + calls Lua's functions*/  voidHellolua::d Emo3 () {lua_state* PL =Lua_open ();        Luaopen_base (PL); /*Execute Script*/lual_dofile (PL,"Hellolua.lua"); /*Put the Helloadd function object on the stack*/Lua_getglobal (PL,"Helloadd"); /*put the parameters required by the function into the stack*/Lua_pushnumber (PL,Ten); Lua_pushnumber (PL,5); /*execution function, the first parameter represents the number of arguments to the function, the second parameter represents the number of function return values, LUA first goes to the stack to take out the parameters, then takes out the function object, starts executing the function*/Lua_call (PL,2,1); intIresult = Lua_tonumber (PL,-1); Cclog ("Iresult =%d", Iresult); }  

Simply explain the steps:

1) Execute script (narrator: I knew you would say nonsense ...) )

2) put the Helloadd function on the stack: Lua_getglobal (PL, "Helloadd"). (Narrator: Look, I knew it!) )

3) Helloadd has 2 parameters, we want to pass the parameters to Lua, so 2 parameters are put in the stack.

4) Steps 2nd and 3rd have put the data needed for the function into the stack, and then just tell Lua to go to the stack to fetch the data and execute the function! Call Lua_call, the comments are very detailed, this is not repeated here.

(Narrator: Too simple point, anyway, no matter what to do, put things in the stack, and then take things from the stack, OK ~ You can roll, after the tutorial I write, you are responsible for spit groove ~)

Come, everyone ignore narration, tutorials or I write better, mainly, spit groove This job, does not meet my identity ~

(Narrator: ...) I have a feeling of being hit.

This is the end of this chapter, is it simple? ~

In the next chapter we describe a function called C + + in Lua!

(Narrator: I like it!) )

Lua and C + + fifth (c + + calls LUA functions)

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.