Luaplus Learning (1)

Source: Internet
Author: User
Document directory
  • Related resource links:
I want to use the Lua script language in my project. We initially knew that Lua was through cegui. Later we didn't plan to use cegui for some reasons, so we had to use Lua separately. It seems simple to see luaplus on the Internet. Record the learning process here.
Can you http://luaplus.org/tiki-index.php here? Page = luaplus + home + page download the Win32 binary file and source code package of luaplus. Extract the header file from the source package and the DLL file from the Binary Package.
Luaplus modifies and encapsulates Lua, making interaction between Lua and C ++ easier. You can find its introduction from the above link.

First, perform two simple tests: (1) Call the functions in the Lua script from C ++; (2) Call the functions in C ++ from the Lua script.

1. Engineering Configuration

In vc7.1, create the Win32 console project testluapluslib.

In the solution view, right-click testluapluslib and select properties. In the configuration combo box, select "All configurations". In turn, click "configuration properties", "C/C ++", and "General ", add the source code to the included directory and decompress it to the path (My: e:/sources/luaplus/luaplus51_build1100/src/luaplus). Click "connector" and "general" in sequence ", the path to which the Binary Package is extracted (My: e:/sources/luaplus/luaplus51_build1100_win32/lib/Win32 ).

Add the following lines of code to testluapluslib. cpp to add the header file and library file.

# Include "luaplus. H"
# If defined (Debug) | defined (_ Debug)
# Pragma comment (Lib, "luaplusd_1100.lib ")
# Else
# Pragma comment (Lib, "luaplus_1100.lib ")
# Endif
Using namespace luaplus;

The project configuration is basically complete.


Ii. Use Lua script for testing

Run the following Lua script for testing and save it in the directory of the executable file you are about to generate. Copy the luaplusd_1100.dll and luaplus_1100.dll in the binary package to this directory. Health = 100;

Printnumber (30 );
 
Function add (x, y)
Return X + Y;
End

Health = 100; the global variables in the Lua script can be accessed by the C ++ file.
Printnumber (30); this statement calls the function in the C ++ file and prints the number to the console window.
Function add (x, y) declares a function. The C ++ file finds and calls this function.

III. C ++ implementation

Add the following two functions to testluapluslib. cpp. 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;
}


Void test ()
...{
// Create State
Luastateowner state;

// With this script can access our own C ++ functions:
State-> getglobals (). Register ("printnumber", printlsnumber );

// Open test file:
Int iret = state-> dofile ("test. Lua ");

// Get a global variable:
Luaobject sobj = state-> getglobal ("health ");
Int mytest = sobj. getinteger ();
Printf ("init value in luascript: % d", mytest );

// Update the value:
Sobj. assigninteger (State, 50 );
// Get value again:
Mytest = sobj. getinteger ();
Printf ("after changed by C ++: % d", mytest );

// Call a function in Lua:
Luafunction <int> Add = state-> getglobal ("add ");
Int myret = add (3, 4 );
Printf ("Call add () in luascript, ret = % d", myret );

}

Main function: int _ tmain (INT argc, _ tchar * argv [])
...{
Test ();
Return 0;
}
Now you can compile and run it after compilation. You can see the result in the console window.
Note that the execution script should be executed after the C ++ function is registered to the Lua module.
Basically. The code for this link (test function) will report an error (COS function in the Math Library is called, but openlibs () function is not called). I modified it, removed the reference to the math library.

Related resource links:

1> luaplus documentation: http://www.gamedev.net/reference/programming/features/lua/
2> luaplus Tutorial: http://wwhiz.com/LuaPlus/LuaPlus.html

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.