Use LuaPlus to integrate the Lua script implementation tutorial in the C ++ Project

Source: Internet
Author: User

UseLuaPlusIntegration in C ++ ProjectsLua scriptImplementation is the content to be introduced in this article, mainly to learnLua script. It took two days to complete integration in the C ++ project.Lua scriptFeasibility analysis. The following job is to writeScriptNow, when writingScriptIntegrate C ++Lua.

At present, there are many implementations of C ++ integration Lua. Most common examples include LuaPlus, LuaBind, ToLua ++, and LuaWrapper. I have tried these methods and have their own advantages and disadvantages.

LuaBind, really good, very powerful. If it doesn't require Boost library support, I will select it.

ToLua ++, I just gave up on it because it was too troublesome to compile. However, the online rating is not bad.

LuaWrapper, developed by the Chinese Predecessors, wanted to support it. However, it can be explained that Boost is still needed and I had to give up.

It is not because Boost is not good, but because the source code of the associated project of my project must be delivered to apply for it. It cannot be ensured that the Boost library is installed on the other machine.

Finally, I choseLuaPlus, I think it is okay. The following is a summary of the use experience.

1. Create a State.

 
 
  1. LuaStateOwner* pState = NULL;  
  2. pState = new LuaStateOwner(true);  
  3. if ( !pState )  
  4.  return E_OUTOFMEMORY; 

Note: The LuaStateOwner constructor parameter true indicates that the Lua standard library is initialized during the constructor. This is very important. At first, I called a constructor without parameters, and then it failed to call math. random () in. lua. I learned it this afternoon.

2. register the C ++ function to Lua (I did not try to reference the example directly)

 
 
  1. static int PrintLSNumber(LuaState* state)  
  2. {  
  3.      LuaStack args(state);  
  4.  
  5.     // Verify it is a number and print it.  
  6.     if (args[1].IsNumber()) {  
  7.          printf("%f ", args[1].GetNumber());  
  8.      }  
  9.     // No return values.  
  10.     return 0;  
  11. }  
  12.  
  13.    LuaObject globalsObj = (*pState)->GetGlobals();  
  14.    globalsObj.Register("PrintNumber", PrintLSNumber); 

3. register the C ++ class member function to Lua

 
 
  1. LuaObject globalsObj = (* pState)-> GetGlobals ();
  2. CTest test; // CTest is the class name.
  3. GlobalsObj. RegisterDirect ("update", test, & CTest: Update );

Update is a member function of CTest,

Update in double quotation marks refers to the function name registered to Lua. In the lua script, update () is used to call the Update () function of the test object.

4. load and execute scripts from files

 
 
  1. int iret = (*pState)->DoFile( "test.lua" );  
  2. if ( 0 != iret )  
  3.  return false; 

If the returned value is 0, the operation is successful. Otherwise, the operation fails.

5. C ++ obtains the variables in the Lua script.

Float x = (* pState)-> GetGlobal ("x"). GetFloat ();

Note: The variable can be obtained from lua only after the DoFile, and the file has been loaded.

6. Call the functions in the script in C ++

 
 
  1. LuaFunction <bool> RandBuild = (* pState)-> GetGlobal ("RandBuild ");
  2. Bool ret = RandBuild ();
  3.  
  4. In the test. lua file:
  5.  
  6. Function RandBuild ()
  7. Math. random (); -- the first random number is too small to discard.
  8. X = math. random (-100,100 );
  9. Return true;
  10. End;

7,Lua scriptCall the C ++ Function

Here, you can call the previously registered C ++ function. when calling the function, you can directly use the function name at registration.

For example, if update () is written in the script in step 1, it is equivalent to test. Update () in C ++ ();

8. Directly calling the registered Lua function in C ++

 
 
  1. (*pState)->DoString( "update()" ); 

This statement only executes the "update ()" script.

In addition, the header files of LuaPlus and Lua, as well as. lib and. dll of LuaPlus, should be put under the project directory. These can be downloaded from the LuaPlus website.

 
 
  1. http://luaplus.org/tiki-index.php?page=LuaPlus+Home+Page 

It's a bit messy.

Summary: UseLuaPlusIntegration in C ++ ProjectsLua scriptThe implementation of the content of the tutorial is complete, hope to learn through this article can help you!

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.