Hot update preparation-Lua core knowledge summary

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
Insert picture description here
Insert picture description here

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
Insert picture description here
output to the console, directly press Ctrl+F5 to run
Insert picture description here

Lua data types

Lua writing does not need to declare data types, just assign values ​​directly
Insert picture description here

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:

  1. Avoid naming conflicts.
  2. Access to local variables is faster than global variables.

Index table
Insert picture description here

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
Insert picture description here
C #
Insert picture description here

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
Insert picture description here
C #

Insert picture description here
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
Insert picture description here
C #
Insert picture description here

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.
Insert picture description here
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
Insert picture description here
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)
Insert picture description here
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:

Insert picture description here
Compared with C#, C# only has two judgments of true and false, and only uses the bool type
Insert picture description here

Lua function

The function in Lua uses function, we can also pass the function as a parameter to the function, as follows:
Insert picture description here
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
Insert picture description here

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.
Insert picture description here
#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
Insert picture description here
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.
Insert picture description here

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)
Insert picture description here
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 ~=
Insert picture description here
and or not [equivalent to && (and) || (or) in C#! (Non)]
Focus on showing the usage of non,
Insert picture description here
operator connection:
Lua does not support + overload to directly perform string splicing, try... for string splicing
Insert picture description here
Insert picture description here
C# directly use + to perform string connection
Insert picture description here

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
Insert picture description here
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 )
Insert picture description here

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
Insert picture description here
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
Insert picture description here

Table method

You can see the specific method, where maxn has been deleted
Insert picture description here

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

Insert picture description here
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
Insert picture description here
Insert picture description here
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)

Guess you like

Origin blog.csdn.net/euphorias/article/details/111186320