LuaPlusLearn how to take notes onC ++IntegrationLuaThe script is the content to be introduced in this article.LuaScriptC ++I am flattered that many friends are interested in packaging and try to use it. As a matter of fact, I have made a lot of defects in packaging and learning. We recommend that you useLuaPlusAsC ++.
LuaPlusYesLuaOfC ++Enhancement, that is,LuaPlusIn itselfLuaSource code. Use itC ++Cooperation is a good choice.
LuaPlus current version: LuaPlus for Lua 5.01 Distribution Build 1080 February 28,200 4 ). You can download to the http://luaplus.org/Site:
- Source code http://wwhiz.com/LuaPlus/LuaPlus50_Build1081.zip)
-
- Target code http://wwhiz.com/LuaPlus/LuaPlus50_Build1081_Win32Binaries.zip)
I will explain how to useLuaPlusAnd how to make it easierLuaPlusAndC ++Class cooperation.
1. Call the Lua script
- // Create a Lua interpreter:
- LuaStateOwner state;
-
- // Execute the Lua script:
- State-> DoString ("print ('Hello World \ n ')");
- // Load the Lua script file and execute:
- State-> DoFile ("C: \ test. lua ");
- // Load the compiled Lua script file and execute it:
- State-> DoFile ("C: \ test. luac ");
-
- 2. Interaction with Lua scripts
-
- // Set variables for Lua scripts
- State-& gt; GetGlobals (). SetNumber ("myvalue", 123456 );
- // Obtain the Lua variable value
- Int myvalue = state-> GetGlobal ("myvalue"). GetInteger ();
-
- // Call the Lua Function
- LuaFunction <int> luaPrint = state-> GetGlobal ("print ");
- LuaPrint ("Hello World \ n ");
-
- // Let Lua call the C language function
- Int add (int a, int B) {return a + B ;}
- State-> GetGlobals (). RegisterDirect ("add", add );
- State-> DoString ("print (add (3, 4 ))");
-
- // Let Lua call C ++ class member functions
- Class Test {public: int add (int a, int B) {return a + B ;}};
- Test test;
- State-> GetGlobals (). RegisterDirect ("add", test, add );
- State-> DoString ("print (add (3, 4 ))");
3.LuaUse in scriptC ++Class
This is a little troublesome. However, I wrapped a LuaPlusHelper. h file, which can easily complete this job. Its implementation is also very simple. You can obtain from the source code how to use pure LuaPlus to implement the same function.
However, there is still a restriction that cannot be solved: Virtual member functions cannot be used. However, considering that we only call the C ++ function in Lua, it is not necessary to import C ++ to Lua perfectly. This restriction is completely acceptable.
In addition, class member variables cannot be directly accessed in Lua, and can be accessed through class member functions, such as SetValue/GetValue ).
- // The following is a simple C ++ class:
- Class Logger
- {
- Public:
- Void LOGMEMBER (const char * message)
- {
- Printf ("In member function: % s \ n", message );
- }
-
- Logger ()
- {
- Printf ("Constructing (% p)... \ n", this );
- V = 10;
- }
- Virtual ~ Logger ()
- {
- Printf ("Destructing (% p)... \ n", this );
- }
-
- Logger (int n)
- {
- Printf ("-- Constructing [% d] (% p)... \ n", n, this );
- }
- Logger (Logger * logger)
- {
- Printf ("-- Constructing [% p] (% p)... \ n", logger, this );
- Logger-> LOGMEMBER ("Call From Constructor \ n ");
- }
- Int SetValue (int val)
- {
- V = val;
- }
- Int GetValue ()
- {
- Return v;
- }
- Public:
- Int v;
- };
-
- // Import the script to Lua:
- LuaClass <Logger> (state)
- . Create ("Logger") // defines the constructor Logger: Logger ()
- . Create <int> ("Logger2") // defines the constructor Logger: Logger (int)
- . Create <Logger *> ("Logger3") // defines the constructor Logger: Logger (Logger *)
- . Destroy ("Free") // defines the Destructor Logger ::~ Logger ()
- . Destroy ("_ gc") // defines the Destructor Logger ::~ Logger ()
- . Def ("lm", & Logger: LOGMEMBER) // defines the member function Logger: LOGMEMBER (const char *)
- . Def ("SetValue", & Logger: SetValue)
- . Def ("GetValue", & Logger: GetValue );
-
- // Use the Logger class in Lua (1 ):
- State-> DoString (
- "L = Logger ();" // call the constructor Logger: Logger ()
- "L. lm ('Hello World 1');" // call the member function Logger: LOGMEMBER (const char *)
- "L. Free ();" // call the Destructor Logger ::~ Logger ()
- );
-
- // Use the Logger class in Lua (2 ):
- State-> DoString (
- "M = Logger (10);" // call the constructor Logger: Logger (int)
- "M. lm ('Hello World 2');" // call the member function Logger: LOGMEMBER (const char *)
- "N = Logger (m);" // call the constructor Logger: Logger (Logger *)
- "N. lm ('Hello World 3');" // call the member function Logger: LOGMEMBER (const char *)
- "M. SetValue (11 );"
- "Print (m. GetValue ());"
- "M, n = nil, nil;" // m, n will be recycled by Lua's vertex pole to call the destructor
- );
4. classify a group of C functions into the Lua module.
- // Like above, I use LuaPlusHelper. h to simplify it:
- LuaModule (state, "mymodule ")
- . Def ("add", add)
- . Def ("add2", test, add );
-
- State-> DoString (
- "Print (mymodule. add (3, 4 ));"
- "Print (mymodule. add2 (3, 4 ));"
- );
5. Use the Lua Table data type
- // Create a Table in Lua
- LuaObject table = state-> GetGlobals (). CreateTable ("mytable ");
- Table. SetInteger ("m", 10 );
- Table. SetNumber ("f", 1.99 );
- Table. SetString ("s", "Hello World ");
- Table. SetWString ("ch", L "hello ");
- Table. SetString (1, "What ");
-
- // Equivalent:
- // Mytable = {m = 10, f = 1.99, s = "Hello World", ch = L "Hello", "What "}
-
- // You can also use table as the key and value:
- State-> GetGlobals (). CreateTable ("nexttable ")
- . SetString (table, "Hello ")
- . SetObject ("obj", table );
- // Equivalent:
- // Nexttable = {mytable = "Hello", obj = mytable}
-
- // Obtain the Table content:
- LuaObject t2 = state-> GetGlobals ("mytable ");
- Int m = t2.GetByName ("m"). GetInteger ();
-
- LuaObject t3 = state-> GetGlobals ("nexttable ");
- Std: string str = t3.GetByObject (t2). GetString ();
6. traverse the Table
- LuaStateOwner state;
- state.DoString( "MyTable = { Hi = 5, Hello = 10, Yo = 6 }" );
-
- LuaObject obj = state.GetGlobals()[ "MyTable" ];
- for ( LuaTableIterator it( obj ); it; it.Next() )
- {
- const char* key = it.GetKey().GetString();
- int num = it.GetValue().GetInteger();
- }
Summary:LuaPlusLearn how to take notes onC ++IntegrationLuaThe script content has been introduced. I hope this article will help you!