Integrated Lua script writers in C : Mu Feng (Second Life members) Copyright reproduced, please indicate the original source Home: Second Life http://www.d2-life.com http: //www.d2-life. COM / LBS / BlogView.asp? logid = 41 Why use Lua as a script? Use Lua as a script, mainly because it is small and exquisite (small size, fast running), and its grammar is relatively simple. However, using Luaapi to integrate Lua Engine into the program, there is indeed inconvenient - in words with the landing of the wind, it is "As a compilation". Of course, now you don't have to worry so hard because you can use Luawrapper for C . Using this tool, integrating Lua scripts in C is a light. Your original C function and class can hardly share with Lua scripts. Let's explain it by example, how to integrate Lua scripts into your program with Luawrapper. 1. Create Lua Engine Luawrap Lua; or Luawrap * Lua = New Luawrap; create a LuawRap object, which is created a Lua script engine. And according to the characteristics of Lua, you can create any plurality of LUA engines, or even distribute them in different threads. 2. Load and execute the script You can load Lua scripts from the buffer: Lua.LoadString ("PRINT ('Hello World')"); of course, you can also load from the file and perform Lua scripts: Lua. LOADFILE ("./ Test.lua"); Lua's script can be a source code or after compiled intermediate code. Maybe you are more interested in compiled middle code - if you don't want to make the source code red naked reveal in front of everyone. 3. Get and set the content of the LUA variable to get and set the script variable, is the most basic function. You can use the getGlobal and setglobal functions to do this: (1) Get variables: int a = lua.getglobal
If there is a function below: int Add (int A, int b) {RETURN A B;} If you want it to allow Lua to use it, you can register it to the Lua engine: Lua.RegisterFunc "add", int, int, add); this way, LUA can be used directly: (Lua script) SUM = Add (1, 3) (*) RegisterFunc's function, let you put C The function is registered into Lua for the Lua script. The first parameter is a function name that wants to use in Lua. The second parameter is the prototype of the function in C ; C allows the function to be overloaded, you can use the function prototype to select the function you need to register into the Lua engine. The third parameter is the pointer of the function in C . 6. How to make C class in Lua to take a look at this C class: class myarray {std :: vector
END_REGLUALIB_MEMBER ()}; as long as these macro definitions, this class is the class that can be used in Lua, we can register this class in Lua: Lua.Register
Such as: lualib_item_create ("New", MyArray) // Create MyArray
This way, you can also create MyArray: a = array.new () in Lua, you can also choose to add a delete object action: lualib_item_destroy ("DEL", myarray) // Remove MyArray, you can delete an object directly. : Array.del (a) (3) Begin_REGLUALIB_MEMBER () ... END_REGLUALIB_MEMBER () here, where you can define the member function of the object, or overload the operator of the object - Yes, it is like C Operator overload. For example: lualib_item_func ("__ newindex", void (MyArray *, int, double), & myarray :: setValue is the overload Operator [] operator. There are still many operators in Lua, such as: __getindex: operator [], support read access, such as v = a [10] __NewDex: operator [], support assignment access, such as A [10] = 1.22 __toString: Convert the variable into a string __add: equivalent to operator __add: operator __sub: operator - __mul: operator × __div: operator ÷ __pow: operator ^ (multiplication) __UNM: One dollar operator - __concat: Operator .. (String Connection) __eq: Operator == (A ~ = B) __lt: Operator = b is equivalent to B <= A, pay attention to, if "__le" is not defined, Lua will try to convert a <= B into NOT (B
There are many compilers that currently support this feature. In the VisualStudo product line, only VC7.1 can support this feature, so you are using VisualStudio, please confirm that you use VisualStudio2003. If you think Luawrapper for C can help you, I will feel very honored. I am willing to share this library to everyone. By the way, if you find bug during use, or have a good suggestion, I hope you can contact me. You are in the process of use, please do not delete signature information in the file; if you modify the library, please add your modification in the modified file. Of course, I will welcome you to feed the modified program. I will continue to optimize and improve it. Need to download Luawrapper for C , please go to http://www.d2-life.com/lbs/blogview.asp?logid=41