Lua Introductory Learning Notes _lua

Source: Internet
Author: User
Tags logical operators lua sublime text

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:

t={["2"]=2, [2]=2.2,}

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:

x = 1
t = {x=1.1, [x]=1}

The first one says

Copy Code code as follows:

t["x"] = 1.1

The second one says

Copy Code code as follows:

T[1] = 1

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.