Article Directory
When it comes to hot updates, Lua is the first to bear the brunt. Here is a summary of Lua's common knowledge points, and then Xlua will be explored and supplemented later. This article is a summary of Lua for hot updates, so I will compare Lua with C#, and I will experience using ILRuntime to implement hot updates in the future. Both will make another summary.
Environment setup
It would be better to write Lua using Rider or vscode during development. VS currently does not have a plug-in to support Lua syntax. Using VS to build a Lua environment is more complicated and less practical. In the learning phase, try to be as fast as possible, so the best is Using vscode, Rider is not free (but future development needs to interact with C#, you can try)
vscode install two plug-ins to start writing lua, convenient and fast
HelloWorld
Single line comment
–
Multi-line comment
– [[
Multi-line comment
Multi-line comment
–]]
Identifier
Global variables
Global variables do not need to be declared. After assigning a value to a variable, the global variable is created. There is no error when accessing an uninitialized global variable, but the result is nil.
By default, variables are always considered global.
If and only if a variable is not equal to nil, the variable exists and
output to the console, directly press Ctrl+F5 to run
Lua data types
Lua writing does not need to declare data types, just assign values directly
Lua variables
There are three types of Lua variables: global variables, local variables, and fields in tables.
Variables in Lua are all global variables, even in statement blocks or functions, unless they are explicitly declared as local variables with local.
The default value of the variable is nil.
Lua Assignment
Lua assigns multiple variables at the same time, without variable transfer, only value transfer:
Local variables should be used as much as possible. There are two benefits:
- Avoid naming conflicts.
- Access to local variables is faster than global variables.
Index table
Lua loop
Lua's loop and C#, the
break statement only exits the loop of the current block (if it is a nested loop, only the innermost loop is exited)
continue to execute the next loop
return to exit all loops
Break: Exit the loop (current layer)
Lua
C #
Continue: execute the next cycle
Execute the next loop, break ends the loop (that is, does not execute the following loop)
There is no Continue syntax in Lua, use goto to achieve the continue effect (goto to continue2 label skip statement print, the effect is the same as continue skip, continue To skip this loop, you can use goto to put the statement that needs to be skipped between goto and the label)
lua
C #
You can see that continue1 is skipped, the subsequent loop is still in progress, and break directly ends the loop (current layer)
return: exit all loops
Exit all loops
Lua
C #
Lua repeat…until loop
The repeat...until loop statement in Lua programming language is different from the for and while loops. The conditional statement of the for and while loop is judged at the beginning of the current loop execution, while the conditional statement of the repeat...until loop is judged after the end of the current loop.
It will not stop until the condition judgment statement (condition) is true.
In C#, there is no repeat statement, and the do while loop is used. This loop is judged after the end, but the content is True before the loop will continue. If it is false, it will end. circulating
the same logic to run only once, i.e., do while run at least once, because the determination after the end of each cycle
so that the reverse can be determined lua (both ends of the loop is a true, one false)
summary : Lua's while loop runs when it is true, C# is the same,
C# do while is also true, and Lua's repeat is false to continue execution (both are judged to be true, but one is true and the other is true. )
Lua process control
The result of the conditional expression of the control structure can be any value. Lua considers false and nil to be false, and true and non-nil to be true.
It should be noted that 0 is true in Lua:
Compared with C#, C# only has two judgments of true and false, and only uses the bool type
Lua function
The function in Lua uses function, we can also pass the function as a parameter to the function, as follows:
The 8th line calls the printing function and passes in a 10 to be printed.
In the 9th line, the function has three parameters, the last parameter is a function, which processes the 3+2 passed in earlier, because the passed in is a printing function, so 5 is printed.
Lua can return multiple values , as long as they are listed after return
C# function returns multiple values :
There are two methods in C# to return multiple values in a function, the keyword is out,
please see this article
https://www.cnblogs.com/roucheng/p/3562327.html
Lua variable parameters
Lua functions can accept a variable number of parameters. Similar to the C language, three points are used in the function parameter list... to indicate that the function has variable parameters.
#You can get the number of data in the table, pass the variable parameters into the table, and then use # to get the number of variable parameters
select("#",...) to directly get the number of variable parameters. (Select('#', …) returns the length of the variable parameter)
select(n, …) is used to return the parameters from n to select('#',…):
when calling select, a fixed actual parameter selector (selection switch) and a series of variable length parameters must be passed in. If the selector is a number n, then select returns its nth variable argument, otherwise it can only be the string "#", so select will return the total number of variable length parameters.
C# Variable parameters
use params object (T) []
(The difference between parameter array and array parameters, a variable number of parameters passed, a passed array, here is a parameter array)
Summary:
Lua will pass variable parameters (...) Then it is processed in the table. C# directly processes the variable parameter array. Lua can also traverse the variable parameters with the select keyword (it is recommended to use the table method, and currently select has no advantage)
lua operator
Use it in C#! = To judge unequal relations, while Lua uses ~=
and or not [equivalent to && (and) || (or) in C#! (Non)]
Focus on showing the usage of non,
operator connection:
Lua does not support + overload to directly perform string splicing, try... for string splicing
C# directly use + to perform string connection
lua iteration
The difference between pairs and ipairs is the
same: both can traverse collections (tables, arrays)
. Ipairs only traverse the values, traversing in ascending order of index, and stop traversing when the index is interrupted. That is, it cannot return nil, only the number 0, if it encounters nil, it will exit. It can only traverse to the first key that is not an integer in the set.
pairs can traverse all elements of the collection. That is, pairs can traverse all the keys in the collection, and in addition to the iterator itself and the traversal table itself, it can also return nil.
ipair will not traverse sequence 0, and will skip
pairs when it encounters nil. Sequence 0 will be traversed and nil will not be skipped, but 0 is placed behind ( try not to use sequence number 0 in the table of lua )
C# uses foreach to traverse
Lua arrays and tables
Use of arrays and tables
Arrays and tables are the most important data structures in Lua. The n
array number starts from 1. Tables are similar to C# dictionaries. When specifying a value, it is an addition to the table value. Remove even if the table is a nil
table and the boundary between an array is not so obvious in Lua , the array can also be used as a table, you can see that we regard the array as a table, as shown
in the figure, add the character as the key and the character as the value The content can also be traversed.
At the same time, the array can be regarded as a table with a key as a number and a value as a string (or other content) ( an array is a special table ).
In addition, the numeric for loop traverses the array with the string aa, which will produce nil
Table method
You can see the specific method, where maxn has been deleted
Table addition, deletion, modification
To add content to the table, use =, delete the direct order to nil, search and use [] and if for judgment, modify and directly assign the value, you
can find that even if the table does not have a serial number, there is no content, you can also use if
In comparison, the C# array subscript starts from 0 , the core of the list is actually an array ( a maintained array ), and the array number is not allowed to be non-numeric.
The dictionary is also in the form of key and value.
You can see that starting from 0 is a. If there is no index, an error will be reported. The
dictionary
can be traversed as long as it is a collection in C#
(this should be enough to deal with hot updates, and I will add it later if necessary)