What is Lua
Lua is a small scripting language. A research group in the Catholic University of Rio de Janeiro, Brazil (Pontifical Catholic University of Rio de Janeiro), by Roberto Ierusalimschy, Waldemar Celes and Luiz Henr Ique de Figueiredo was formed and developed in 1993. It is designed to be embedded in the application, providing flexible extension and customization capabilities for the application. LUA is written in standard C and can be compiled and run on almost all operating systems and platforms. LUA does not provide a powerful library, which is determined by its positioning. So Lua is not suitable as a language for developing standalone applications. Lua has a simultaneous git project that provides instant compilation capabilities on a specific platform.
Lua scripts can be easily called by C/C + + code, or they can be used in turn to C + + functions, which makes LUA widely available in applications. Not only as an extension script, but also as a normal configuration file, instead of the Xml,ini file format, and easier to understand and maintain. LUA is written in standard C, and the code is simple and beautiful, and can be compiled and run on almost all operating systems and platforms. A complete LUA interpreter but 200k, the speed of Lua is the fastest in all of the scripting engines available. It all determines that Lua is the best choice for embedded scripting.
the notes in Lua
Single-line Comment
Use two minus signs to indicate the beginning of a comment and continue to the end of the line. Equivalent to "//" in C language.
--Here is a line of comment print ("Hello Lanou")
Multi-line comments
Use "--[[" to start the comment, using "]" to indicate the end of the comment. This annotation is equivalent to "/*" and "/*" in C language.
--[[here is the first line of comments here is the second line of comment]]pring ("Hello Lanou")
the variables in Lua
By default, LUA considers all variables to be global variables. If you need to define a local variable, you need to use the local description when declaring the variable.
And at the time of assignment, multiple variables are allowed to be assigned at the same time.
---I is a local variable, local i = The name is a global variable name = "Lewis"--age,height are local age,height = 34,183.5--gender,company are global variables Gen Der,company = "Male", "Blue Gull"
the operators in Lua
Arithmetic operators
--+ (ADD) print (1 + 2)---(subtraction) print (1-2)--* (multiplication) print (1 * 2)--/(Division) print (1/2)--^ (powers) Print (1 ^ 2)
Comparison operators
--< (less than) print (1 < 2)--> (greater than) print (1 > 2)--<= (less than or equal) print (1 <= 2)-->= (greater than or equal) print (1/2)--= = (equals equals) pri NT (1 = = 2)--~= (not equal to) print (1 ~= 2)
logical operators
The use of logical operators differs greatly from the C language.
In the Lua language, only false and nil are false, and any other data is true,0 and true!!!
The operation result of and and or is not true and false, but is related to his two operands.
A and B: Returns a If A is false, otherwise returns B;
A or B: If A is true, A is returned, or b is returned;
--and (with) print (1 and 2)--or (or) print (1 or 2)--not (non) print (1 not 2)
With this feature in Lua, you can simulate the C language?: operator
For example: X=a?b:c in C, in Lua, can be written as x = A and B or C.
data types in Lua
Key words |
Describe |
Nil |
Null value, all unused variables are nil;nil both value and data type |
Boolean |
Boolean type, only two valid values: True and False |
Number |
Numeric type, in Lua, equivalent to the double in C language |
String |
String, if you wish, the string is a literal that can contain " |
Table |
Relationship type, this type of function is more powerful |
function |
function type, which can be declared by a function type |
UserData |
This type deals specifically with the host of Lua. The host is typically developed by the C and C + + languages, in which case the UserData can be any type of host, often a struct and pointer type |
Thread |
Thread type, there is no real thread in Lua. |
code blocks in Lua
In the C language, blocks of code are enclosed in "{" and "}", in Lua, using the Do and end keywords.
Do print ("Hello") end
types of relationships in Lua
Relationship type, is a very powerful type. This type is similar to the mapping structure in the C + + language, similar to the array object in the PHP language, similar to the Nsdictionary object in OC.
The relationship type (Table) definition in Lua is simple, and its main feature is to use "{" and "}" to enclose a series of elements.
--Declaring a global variable of a relationship type tabletable = {}--Assignment relationship variable the value of table index to 0 o'clock is 34table[0] = 34--can also be written as Table.name = "Lewis" table["name"] = "Lewis" Table.gender = "Male"-you can also write an object indexed as "son" into another table table["Son" = {name = "DD", gender = "male"}--access to the object can use print (table.son.name )
You can also declare as follows
--Declare the relationship variable tabletable = {ten,--equivalent to [1] = 10[100] = 40,lewis = {--also can be written as ["Lewis"] = Age = 34,--also can be written as ["age"] = 34gender = "Male",},20,--equivalent to [2] = 20}print (table[2])
the functions in Lua
In Lua, the definition of a function is very simple. However, it is important to note that the return statement must be written before end. If we have to add a return statement to the function, it should be written
Do--return statement Be sure to return end before a block of code ends
Examples of functions
--Declaring the function type variable sumfunction sum (v1,v2)--function body return v1 + v2end--The same function variable can also be declared as follows Mul = function (v1,v2)--function body return v1 * v2end-- Call the first function sumprint (sum (2,3))--Call the second function mulprint (Mul (3,4))
the classes in Lua
As I said before, table types can have any type of value, including functions!
Therefore, we can create a table with function variables.
Lewis = {age = 34,work = function (self,message)--function Body Self.age = self.age + 1;print (self.age. Message) End}print (lewis.ag e) Lewis.work (Lewis, "work")--you can also call method Lewis:work ("work") as follows
End
Like the C language, LUA provides a number of standard libraries to enhance the functionality of the language. With these functions, it is convenient to manipulate various data types.
You can refer to the book "Programming in Lua".
[Lua] Getting Started tutorial