Lua Game Development Practice Guide Study Notes 1

Source: Internet
Author: User

This article provides some learning notes based on the Lua Game Development Practice Guide. It is only used to continue some of your learning knowledge.

Lua Basics

1. Language definition:

In Lua, identifiers have a lot of flexibility (variables and function names). However, you must avoid the use of underscores (_) to upper-case letters when using numbers as the start character, because this format is reserved by Lua itself, such as _ start.

We recommend that you use the following format and naming rules to define variables, constants, and function names:

① Constants are capitalized and underlined, for example, my_constant.

② The first letter of the variable is lowercase, for example: myvariable

③ The first letter of the fullwidth variable is in lowercase G, for example, gmyglobal.

④ The first letter of the function name is capitalized, for example: functionmyfunction ()

In Lua, you can mark the behavior comments of the row in the first two (--) characters of a text segment. You can also use block comments. refer to the following example:

-- This is acomment in Lua that is on its own line

Myvalue = 7 -- You can also add a comment to a line of script

2. variables:

In Lua, variables do not need to be declared before use. This is a bit controversial. Because no declaration is required, the required variables can be introduced anywhere. The following code allows you to create, classify, and assign values to variables:

Myvalue = 7;

In this Code, a variable named myvalue is created and assigned a number 7. You can use the type function to determine the type of the variable. There are five types of variables in Lua: nil, Boolean, String, number, and table.

Nil is a simple type used to indicate that the variable has not been assigned a value (refer to the following example). If a variable is assigned a value of nil, it actually means to delete the variable.

Myvalue = nil-This deletes the variable

Local myvalue-this creates a local variable with aninitial nil value

There are only two types of Boolean variables: true and false. Boolean variables are commonly used in condition representation. refer to the following example:

Myvalue true-creates a Boolean variable with a value oftrue

The string type is relatively simple in Lua. The Lua string can contain only one character or more than one million characters.

Number is a double-precision floating point number in Lua. Lua has no INTEGER (the integer type is not required because the value smaller than 1w14 does not have an integer). numbers can be expressed in the following ways:

Mynumber = 7

Mynumber = 0.756

Table is the most powerful and vulnerable data type in Lua. We can use it as an array.

Test the following code on the console:

Ready> mytable = {2, 4, 6, 8, 10}

Ready> Print (mytable [3])

6

In this example, the table-related functions look like an array that stores many variables. We use brackets and indexes to obtain values in the table.

3. Local and global variables

The Lua variable is global by default, that is, the value of the variable remains unchanged throughout the session unless the script changes it. When a global variable is used, a G letter is added before the variable name. Defining a local variable can set an initial value or not. See the following example:

Function myfunction ()

Local myx = 7

If myx <10 then

Local myy = "Hello World"

End

Print (myy );

End

4. Operators

An operator is a special symbol that makes two values worth the calculation result. Arithmetic Operators can get the calculation result, and Relational operators get the result of Boolean.

Arithmetic Operator. Lua supports the following standard arithmetic operators:

Add, for example, a + B = C;

Cut, for example, a-B = C

Multiplication, for example, a * B = C

Except, for example, A/B = C

Relational operators

You can use the following standard Relational operators to compare values or expressions:

If a = B then

Prinf ("A is equal to B ")

End

If a-= B then

Print ("A is not equal to B ")

End

If a <B then

Print ("A is less than B ")

End

If A> B then

Prin ("A is greater than B ")

End

Logical operators

The logical operators test two parameters and return correlated results. In Lua, logical operators Use lowercase letters.

The and operator compares two parameters. If one parameter is false, false is returned; otherwise, the value of the first parameter is returned.

A = 5

B = 10

C = 20

If (A <10) and (B <20) then

Print ("This returns true ")

End

If (A> C) AND (B <c) then

Print ("This returns false ")

End

5. Control Structure

Lua contains a small number of important control structures, allowing you to have full options in the script. All control structures can be used as end labels.

If statements are common. If you are familiar with other programming or scripting languages, you must have seen them many times. If language can judge a parameter. If it is true, the program block will be executed. For example:

Myvalue = 7;

If myvalue <10 then

Print ("myvalue is less than ten ")

End

If (myvalue> 5) and (myvalue <10) then

Print ("myvalue is between five andten ")

End

We can also use else and another program block to extend the if statement function. If the if statement returns false, the program block enclosed by else-end is executed. For example:

Myvalue = 20

If myvalue = 21 then

Print ("The value is 21 ")

Else

Print ("The value is not 21 ")

End

In addition, you can use the elseif keyword to add a series of conditional statements. In AI scripts, this keyword is not sincere because Lua does not support case clauses. For example:

Myvalue = 17

If myvalue <6 then

Print ("myvalue is between zero andfive ")

Elseif myvalue <11 then

Print ("myvalue is between six andten ")

Elseif myvalue <16 then

Print ("myvalue is between elevenand twenty ")

Elseif myvalue <21The

Print ("myvalue is between sixteenand twenty ")

Else

Prin ("myvalue is greater thantwenty ")

End

While and repeat

The while and repeat control structures are very similar. Both of them can execute a script cyclically to know that a condition is met. The while control structure first determines a parameter. If the conditional is true, the program block will be executed (or it may never be executed ). The repeat control structure determines the parameters at the end, which ensures that the program block is executed at least once.

The while control structure uses the do keyword, which is the same as the then keyword of the IF control structure. It is used to mark the start of a conditional block. See the following example:

Index = 1

While index <10 do

Prinf ("loop pass:", index)

Index = index + 1

End

For control structure

Lua provides two for control structures (digital and general). Here we first explain the digital control structure. The for control structure allows you to execute a script for a limited number of times based on the value of the expression. For example:

For Index = 1, 10 do

Print (INDEX)

End

After the for keyword, you need to provide the variable range. When traversing every value in the range, the program block will be executed. The do keyword indicates the start of the block and the end indicates the end of the block.

You can define the value of "Step" in the third parameter. For example:

For Index = 10, 1,-1 do

Print (INDEX)

End

For Index = 1,100, 2 do

Print (INDEX)

End

When using the for control structure, you need to pay attention to the following points. First, the number of cycles is determined only when the first execution is performed. Therefore, even if you change the value of the parameter, the number of cycles is not affected. Second, the variable in the loop structure is a local variable. Once the loop ends, it will be clear. To save their values, you must use global variables or higher-level local variables.

Break Control Structure

The break statement can be forcibly exited from the loop control structure. The user cannot recycle it, and it must be at the end of the program block (usually the if-then statement ). See the following example:

For Index = 1,100 do

If Index = 52 then

Print ("52 -- ouch ")

Break

End

Print ("The value is", Index ')

End

Print ("This is the that will be executed after the break ")

 

Lua Game Development Practice Guide Study Notes 1

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.