Lua scripting: Getting started with the Lua language

Source: Internet
Author: User
Tags lua

Lua is a simple and powerful language, and its own strong extensibility makes this language play an important role in the game design and other fields. Bloggers have used the language in Unity3d, and have studied and discussed the content of LUA and unity, C + +, and more. Recently, because of the details of the Lua script in the book Advanced Programming for Game scripting, the blogger's reading experience is recorded here, which is convenient for later viewing when needed.

LUA System composition

The LUA system consists of three parts: the LUA link library, the Luac compiler, and the LUA interpreter.
* The LUA link library consists mainly of two files, Lua.lib and lua.h. The LUA link library is primarily responsible for initializing and closing its own operations, loading scripts and executing scripts,
For callable Interactive interfaces.
* The Luac compiler is a command-line-driven compiler with a name of Luac. When we need to compile a script using the Luac compiler, just enter

>luac <FileName>    //FileName为脚本名称

We can load the script directly from the LUA link library and dynamically compile it during loading, but this can cause two problems, that is, the inability to get error information and dynamic compilation during dynamic compilation makes the script slow to load and should be noticed when used.
* LUA interpreter is a command-line driven code runtime environment where we can run and test script code directly in this environment.

LUA scripting syntax
    • Note: The comments for the Lua script begin with – as
--这是一句注释

When we need to annotate multiple lines of script, we can take a manual line-wrapping method to make multiple lines of comments.
* Variables: Variables in LUA scripts are untyped, implicitly declared, first characters must be non-numeric characters, case sensitive. One important feature of variables in LUA scripts is the support for multiple assignments, which allows multiple variables to be written to the left of the assignment operator. Such as

-- 变量个数等于数值个数x,y,z=1,2,3-- 变量个数大于数值个数,z的值为nilx,y,z=1,2-- 变量个数小于数值个数,3这个数值将被忽略x,y=1,2,3
    • Data type: 6 data types are supported in Lua, that is, number, string, function, table, user data (UserData), and null (nil).
A number refers to integer and floating-point data. Stringstring) refers to data of a string type. function function)refers to a formal declaration of a function's reference. Such as: function fib(n)   if(n<2) Then     returnNElse    returnFIB (n1) +FIB (n2)EndEnd--functions can be assigned to variables in LuaFib2=fib--Call the FIB functionPrint(Fib2 (5)) Table (TableIs the simplest and most complex data structure in the LUA language: simple as an ordinary array, complicated as a list, dictionary, class, etc.--We do not need to specify the data type and data size when constructing a data collection--The default index of the data collection after the initialization is complete starts at 1 unless the value at index 0 is declared--Constructs an array of numeric typesintarray={1,2,3,4,5}--Constructs an array of type stringstringarray={"A","B","C","D"}--Print the first element of the Intarray with an output of 1Print(intarray[1])--Displays the value of the declaration Stringarray at index 0stringarray[0]="E"--Prints the first and second elements of the Stringarray and outputs the E,aPrint(stringarray[0],stringarray[1])--Print an array value that is out of bounds, output to nilPrint(intarray[Ten])--The data type of the table in Lua can be differentTable[0]="Table"Table[1]=1--The table's index in LUA can be any type, because the table is based on the key-value principleenemy={}enemy["Name"]="Enemy"enemy["HP"]= -enemy["Speed"]= ---in particular, if Key is a valid string type, then Table[key] is equivalent to Table.key. Enemy={}enemy.name="Enemy"enemy.hp= -Enemy.speed= -User data (UserData) is a special data type in the Lua language that allows pointers in the C language to be stored in the variables of the Lua script. Null value (Nil) is a common data type in a variety of languages and is not described here. In Lua scripting we can use thetype() function to get the type of any data
    • Logic and expression: Lua and most of the programming are similar to support subtraction operations, the difference is that the use of ~= in Lua to denote unequal relations.
      The conditional logic supported by LUA is primarily if-then-else and nested If-then-else,lua do not support the switch structure. The loop structure supported by LUA consists of the while, for, and REPEA three structures, such as:
--This is a while loopI=0 while(i<Ten) Doi++ Print (i)End--This is a for loop forI=0,Ten  DoPrint (i) End --This is a repeat cycleRepeatPrint (i) i++until(i>Ten)--This is an extended for loop, similar to the foreach structure, used primarily to traverse tables (table) forKeyvalue inchTables DoPrint (k,value)End
LUA interacts with C + +

To update, and so on after the study to update it, compiled Lua5.3 has not been successful .....

Lua scripting: Getting started with the Lua language

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.