Recently, we've been using Cocos2d-x + LUA to develop the game. The main logic of the game will be written in Lua, with no prior contact with Lua, and here are the introductory notes I've summarized.
Operator
logical operators
With: and
Or: OR
Non: not
Logical judgments are false and nil only, and the rest are true. Or and and will return the value of the first open circuit.
There is no C-a:b in Lua, but there is an alternative (x and a) or B.
It should be noted that the above scenario is the opposite of the three-character result when X is true,a to False,b to true.
Relational operators
Not equal to: ~=
Both types and values are equal: = =
Only numbers and string types determine values when compared, and the rest data types compare references.
Other operators
Connector for string:..
When two strings are connected with a +, they are automatically escaped to add to the numeric type, which is determined by the add operation in the metatable.
Length Operator: # Calculates the length of a string and a table.
Colon operator:: is a LUA syntax sugar that eliminates the method's self parameter declaration and explicit invocation of the method. See: Http://www.lua.org/pil/16.html.
Table
The table is visible as an array and the subscript is different from most other languages, with the subscript starting at 1.
#运算符仅统计所有下标为整数的元素的个数.
Unlike pairs, ipairs only iterates through successive subscripts starting at 1 and guarantees the output order.
T[2] and t["2"] indicate different records
Copy Code code as follows:
The table can be left with a separator (or;), referring to the previous T, and the official reason is to automatically generate the table's convenience (easy to generate LUA table configuration tables automatically).
Copy Code code as follows:
The first one says
Copy Code code as follows:
The second one says
Copy Code code as follows:
MetaTable
The type of metatable is table. The key in the MetaTable is the event (action) type. The value in MetaTable is the corresponding function (method), also called Metamethod.
Control flow
The return and break statements can only be written in the last sentence of the statement block, which is before the end.
The loop variable for the for loop is an internal variable that cannot be used again after the loop has ended.
Sublime Text 2 compilation
I usually write the Lua script with sublime text, and its compilation Lua setup is as follows: Generate Lua.sublime-build file in C:\Users\YOUR_USER_NAME\AppData\Roaming\Sublime text 2\packages\lua directory, the contents of the file is
Copy Code code as follows:
{
"cmd": ["Lua", "$file"],
"File_regex": "^ (?: Lua:)? [ \ t] (... *?):( [0-9]*]:? ([0-9]*) ",
"Selector": "Source.lua"
}
When set, use CTRL + B to compile and run. The above is the configuration on the PC.
Need to compile and install Lua on Mac
Copy Code code as follows:
Curl-r-O http://www.lua.org/ftp/lua-5.2.2.tar.gz
Tar zxf lua-5.2.2.tar.gz
CD lua-5.2.2
Make MACOSX Test
sudo make install
Then create a new lua.sublime-build in sublime text tools-> build system-> New build system, which is
Copy Code code as follows:
{
"cmd": ["/usr/local/bin/lua", "$file"],
"File_regex": "^ (... *?):( [0-9]*]:? ([0-9]*) ",
"Selector": "Source.lua"
}
You can compile Lua with ⌘+ B on the Mac's sublime text.
Other
A semicolon after a LUA statement;
Variable declarations are declared as much as possible using local, unless there is a need to be a global variable.
As a list of arguments, fetch ARG in the function with ARG (type table) and an extra key n to indicate the number of parameters.