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.
- LuaStateOwner* pState = NULL;
- pState = new LuaStateOwner(true);
- if ( !pState )
- 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)
- static int PrintLSNumber(LuaState* state)
- {
- LuaStack args(state);
-
- // Verify it is a number and print it.
- if (args[1].IsNumber()) {
- printf("%f ", args[1].GetNumber());
- }
- // No return values.
- return 0;
- }
-
- LuaObject globalsObj = (*pState)->GetGlobals();
- globalsObj.Register("PrintNumber", PrintLSNumber);
3. register the C ++ class member function to Lua
- LuaObject globalsObj = (* pState)-> GetGlobals ();
- CTest test; // CTest is the class name.
- 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
- int iret = (*pState)->DoFile( "test.lua" );
- if ( 0 != iret )
- 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 ++
- LuaFunction <bool> RandBuild = (* pState)-> GetGlobal ("RandBuild ");
- Bool ret = RandBuild ();
-
- In the test. lua file:
-
- Function RandBuild ()
- Math. random (); -- the first random number is too small to discard.
- X = math. random (-100,100 );
- Return true;
- 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 ++
- (*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.
- 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!