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.
- Free -- free things are used by everyone;
- Small-the kernel is smaller than 860 KB, and python is about KB;
- Fast-execution speed is the most surprising thing;
- Easy to transplant
- 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:
- Reserved keywords
- Syntax
- Variable
- Operator
- 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 --
- 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
- 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
- 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
Table Data Structure
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