Summary of Lua Game Development Practice Guide

Source: Internet
Author: User
Document directory
  • Function
  • Standard Library
  • Character Processing
  • Table Data Structure
  • I/O Basics
Why use Lua?
Core objective: scalability. The kernel remains unchanged because it is mainly scalable.
  1. Free -- free things are used by everyone;
  2. Small-the kernel is smaller than 860 KB, and python is about KB;
  3. Fast-execution speed is the most surprising thing;
  4. Easy to transplant
  5. Integration with C/C ++
What can Lua do?
  • Edit the game user interface
  • Define, store, and manage basic game data
  • Manage real-time game events
  • Create and maintain a developer-friendly game storage and loading system
  • Compile the AI system of the game
  • Create a functional prototype and port it in a high-performance language.
Generally speaking, the Lua language involves the following programming languages:
  1. Reserved keywords
  2. Syntax
  3. Variable
  4. Operator
  5. Control Structure
Retained the keyword-this is not to be mentioned, so pay attention to it when naming it. Syntax-this is not enough, and the operation is getting familiar with it. There are only five types of variables in the variable -- Lua: nil, Boolean, String, number, and table operators -- this is basically an arithmetic operator (addition, subtraction, multiplication, division, and so on), Relational operators (greater than or equal to not equal to. Here we emphasize that objects of the table type are equal only when two variables are the same object), logical operators (true or false) control Structure --
  1. Condition Statement:
    if 2>1 then    print("2>1")end
    myValue = 20if myValue < 6 then    print("myValue is less then 6")elseif myValue < 12 then    print("myValue is less then 12")else    print("myValue is greater then 12")end
  2. Loop statement:
    indx = 1while index < 10 do    print("loop at index: ", index)    indx = indx + 1end
    index = 1repeat    print("loop at index: ", index)    indx = indx + 1until indx >10

    for indx = 10, 1, -1 do    print("loop at index: ", indx)endfor indx = 1, 10 do    print("loop at index: ", indx)end
  3. Interrupted
    for indx = 1, 100 do    if indx ==55 then        print("55, found!")        break    end    print("loop at index: ",indx)end

Deep Dive into Lua
  • Function
  • Standard Library
  • Character Processing
  • Table Data Structure
  • I/O Basics
Simple Functions
function foo()    print("Hello World!")end

Single Parameter

function foo(varString)    print("the string is: ", varString)end

Multiple Parameters

Function Foo (varname, varage) print ("the name is:", varname) print ("the age is:", varage) end -- Lua... it indicates that multiple parameters are passed. The local table variable Arg is used to save function Foo (...) if Arg. n> 0 then for indx = 1, Arg. n do local mystring = string. format ("index: % d the value: % s", indx, Arg [indx]) end else print ("no variables entered. ") endend
function foo(var1, var2, ...)    local myString    if arg.n == 0 then        myString = string.format("var1: %s    var2: %s", var1, var2)    else        myString = string.format("var1: %s    var2: %s    var3: %s", var1, var2, arg[1])    end    print(myString)end

Return Value

Function Foo () myvalue = "Hello world! "Return myvalueend -- multiple return values. You only need to use commas to separate function Foo () d1 = math. random (1, 5) D2 = math. random (1, 5) D3 = math. random (1, 5) mytotal = D1 + D2 + D3 return D1, D2, D3, mytotalend
Standard Library assert (myvalue) () -- run the compiled Lua code block. (Not quite clear)
a = "hello world"b = "print(a)"assert(loadstring(b))()

Dofile (filename) -- load and execute the Lua script file

dofile("scripts/runtime_functions.lua");

Math. Floor () math. Random ()

myValue = math.random(1,5)
Math. Min () Character Processing
  • Type conversion
  • String. Char (N1, N2,...) -- returns the character of the input parameter based on the ASCII code
  • String. Len (mystring) -- returns the string length.
  • String. sub (mystring, start, end) -- returns the string substring from start to end in mystring. In particular, when start is negative, such as-5, in this case, the last five digits are returned, that is, the end parameter can be omitted from the tail end to the-5 position.
  • String. Format () -- % d, % s, %. 2f
  • String. Find (sourcestring, findstring)
    sStart, sEnd = string.find("my name is John Smith.", "John")

  • String. gsub (sourcestring, pattern, replacementstring) -- this is a replacement function and returns a string after replacement. Characters in sourcestring that meet the pattern format will be replaced with replacementstring (note that when matching the pattern format, the matching will be terminated if a space or comma is encountered)
  • String. gfind (sourcestring, pattern) -- this is a function for searching substrings. A table object is returned, and all substrings matching pattern are stored in the table.
Table Data Structure
  • Table. getn (mytable)
  • Table. insert (mytable) (mytable, position, value)
  • Table. Remove (mytable, position) -- deletes the element at the specified position. If position is not specified, the last element is deleted by default.
  • Pairs () traversal-table is more like a key-value pair, so we can understand pairs () traversal.
    myTable = {"One", "Two"}for index, value in pairs(myTable) do    print(index, value)end

I/O Basics
Myfile = Io. Open ("test_data.lua", "W") -- IO. open () two parameters are: File Name and output mode if myfile ~ = Nil then myfile: Write ("---- I write this string to the file. ----") Io. Close (myfile) End

Integration of Lua and C/C ++ programs is not complete yet

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.