注釋: 寫一個程序,總是少不了注釋的。在Lua中,你可以使用單行注釋和多行注釋。
單行注釋:連續兩個減號“–”表示注釋的開始,直到行末為止。 多行注釋:由“–[[” 表示注釋開始,并 且一直延續到“–]]”為止。 快捷方式是Ctrl+k+c Ctrl+k+u
function fact (n )if n ==
0 then return 1 else return n * fact(n-
1 )
end
end
print(
"enter a number:" )
a = io.
read ()
print(fact(
a ))
暫停
os .execute(
"pause" )
標識符: 字母(letter)或者下劃線開頭的字母、下劃線、數字序列。最好不要使用下劃線加大寫字母的標識符。
注意:Lua大小寫敏感。 合法的標識符 非法標識符 HelloWorld and(關鍵字) _983 983(純數字) _name hello world(特殊符號)
保留字: Lua語言中一些賦以特定的含義,用做專門用途的字符串。以下字符為Lua保留字,不能當做標識符。
and break do else elseif end false for function if in local nil not or repeat return then true until while
變量: 變量在使用前,必須在代碼中進行聲明,即創建該變量。
編譯程序執行代碼之前編譯器需要知道如何給語句變量開辟存儲區,用于存儲變量的值。
Lua 變量有三種類型:全局變量、局部變量、表中的域。
Lua 中的變量全是全局變量,那怕是語句塊或是函數里,除非用 local 顯示聲明為局部變量。
局部變量的作用域為從聲明位置開始到所在語句塊結束。
變量的默認值均為 nil。
基本數據類型: Lua是動態類型語言,變量不用類型定義。(類型的檢查是在運行時做的) a=5 print(a)
Lua中定義了8個基本類型: nil(空)、boolean、number(所有數值類型)、string、userdata、 function、thread和table
function fact (n )if n ==
0 then return 1 else return n * fact(n-
1 )
end
end
print(
"enter a number:" )
a = io.
read ()
print(fact(
a ))
print(
a )
print(type(
a ))
b =
""
if (b)
then
print(
"true" )
else
print(
"false" )
end
a =
123
b =
1.23
c =
1.23456789
d = -
123
print(type(
a ))
print(type(b))
print(type(c))
print(type(d))
a =
"asdf"
b =
'asdf'
print(
"10" +
1 )
print(
'10' +
'1' )
print(
"a" .
.1 )
c=
"asdf\nghjk"
print(c)
a =
'aaBBccDD'
print(
string .
upper (
a ))
print(
string .
lower (
a ))
print(
string .gsub(
a ,
'cc' ,
'zz' ,
2 ))
print(
string .
len (
a ))
print(
string .find(
a ,
'DD' ))
print(
string .find(
a ,
'dd' ))
print(
string .sub(
a ,
1 ,
2 ))
function Debug (a )
print(
a )
end
Debug(
"asdfasdg" )
a =Debug
print(
a )
print(type(Debug))
function Foo ()
return Debug
end b =Foo
print(b)
function Debug (a )
print(
a )
end b=Debug
function Foo (b )
b(
"asdf" )
end Foo(b)
print(Foo(b))
print(b)
function Foo ()
a =
10
local b =
20
end Foo()print(
a )
print(b)
a =
10
b=
20
if (
a ~=b)
then
print(
"a!=b" )
else
print(
"a==b" )
end
if (
true )
then
print(
'AAA' )
end if (
true )
then
print(
'true' )
else
print(
"flase" )
end a =
10
if (
a <
10 )
then
print(
'aaa' )
elseif(
a <
20 )
then
print(
'BBB' )
elseif(
a <
30 )
then
print(
'CCC' )
end
a =
0
while (
a <
10 )
do
a =
a +
1
print(
a )
end
a =
0
repeat
a =
a +
1
print(
a )
until (
a ==
10 )
for i=
0 ,
100 ,
3 do
print(i)
end for i =
0 ,
100 ,
1 do
if (i%
3 ==
0 )
then
print(i)
end
end
a ={
5 ,
6 ,
4 ,
3 ,}
color ={
"red" ,
"green" ,
"blue" ,
"black" }
for k,v
in ipairs(
a )
do
print(k,v)
end for k,v
in ipairs(color)
do
print(k,v)
end
for i =
0 ,
10 ,
1 do if (i==
3 )
then break
end print(i)
end
a ={
8 ,
9 ,
5 ,
6 ,
7 }
for key,
value in ipairs(
a )
do print(key,
value )
end
color ={aa=
"red" ,bb=
"green" ,cc=
"blue" ,dd=
"black" }
for key,
value in pairs(color)
do print(key,
value )
end color ={aa=
"red" ,
"green" ,
"blue" ,
"black" }
for key,
value in pairs(color)
do print(key,
value )
end
arr ={
2 ,
3 ,
4 ,
1 }
print(table.concat(arr,
"==" ))
table.insert(arr,
2 ,
100 )
print(table.concat(arr,
"==" ))
table.remove(arr,
1 )
print(table.concat(arr,
"==" ))
table.
sort (arr)
print(table.concat(arr,
" " ))
arr ={
21 ,
23.45 ,-
456 ,
123465789798 }
arr[
5 ] =
132456
print(arr[
2 ])
for key,
value in pairs(arr)
do
print(key,
value )
end
color={r=
"red" ,g=
"green" ,b=
"blue" }
print(color.b)
print(color.g)
color.
a =
"alpha" for key,
value in pairs(color)
do print(key,
value )
end
List ={
"aa" ,
"bb" ,
"cc" ,
"dd" }
function ListAdd (str )table.insert(List,str)
end ListAdd(
"ee" )
ListAdd(
"ff" )
print(table.concat(List,
" " ))
function ListRemove (index )table.remove(List,index)
end
ListRemove(
2 )
ListRemove(
2 )
print(table.concat(List,
" " ))
Queue ={
"aa" ,
"bb" ,
"cc" ,
"dd" }
function Enqueue (str )table.insert(Queue,str)
end
function Dequeue ()result =Queue[
1 ]table.remove(Queue,
1 )
return result
end Enqueue(
"ee" )
print(table.concat(Queue,
" " ))
Enqueue(
"ff" )
print(table.concat(Queue,
" " ))
Dequeue()
print(table.concat(Queue,
" " ))
Dequeue()
print(table.concat(Queue,
" " ))
Stack ={
"aa" ,
"bb" ,
"cc" ,
"dd" }
function Pull (str )table.insert(Stack,str)
end
function Push ()lastIndex =Stack.
len result =Stack[lastIndex]table.remove(Stack,lastIndex)
return result
end Pull(
"ee" )
print(table.concat(Stack,
" " ))
Pull(
"ff" )
print(table.concat(Stack,
" " ))
Push()
print(table.concat(Stack,
" " ))
Push()
print(table.concat(Stack,
" " ))
function Foo ()local a =
"AAA" function Foo1 ()print(
a )
end Foo1()
end
Foo()
MyMath ={}
MyMath.Add =
function (a ,b )return a + b
end
MyMath.Sub =
function (a ,b )return a - b
end
MyMath ={Add=
function (a ,b )return a + b
end ,Sub=
function (a ,b )return a - b
end
}
MyMath ={}
function MyMath .Add (a ,b )return a + b
end
function MyMath .Sub (a ,b )return a - b
end print(MyMath.Add(
1 ,
1 ))
print(MyMath.Sub(
2 ,
2 ))
Action ={}
function AddListener (func )table.insert(Action,func)
end
function Invoke ()for i =
1 ,
3 ,
1 do Action[i]()
end
end a =
function () print ("aa" )end
b =
function () print ("bb" )end
c =
function () print ("cc" )end AddListener(
a )
AddListener(b)
AddListener(c)
Invoke()
Weapon ={}
Weapon.name =
"Ak47"
Weapon.attackDistance =
20
Weapon.hurt=
10
Weapon.Fire =
function (hurtValue ) print ("Ak47 fire" ..hurtValue ) end
Weapon.FireEffect =
function () print ("AK47 fire effect" )end print(Weapon.name)
Weapon.FireEffect()
Weapon.Fire(Weapon.hurt)
CreateWeapon ={}
CreateWeapon.Weapon =
function (name ,attackDistance ,hurt )Weapon.name =
"Ak47" Weapon.attackDistance =
20 Weapon.hurt=
10 Weapon.Fire =
function (name , hurtValue ) print (name .."fire" ..hurtValue ) end Weapon.FireEffect =
function (name ) print (name .. " fire effect" )end end
ak47 =CreateWeapon.Weapon(
"ak47" ,
"20" ,
10 )
m16 =CreateWeapon.Weapon(
"m16" ,
"30" ,
30 )ak47.FireEffect(ak47.name)
ak47.Fire(ak47.name,ak47.attackDistance)
a =
require (
"MyMath" )
print(
a .Add(
1 ,
2 ))
print(
a .Sub(
1 ,
2 ))
a .PublicFoo()
a .PrivateFooEnter()
os.execute(
"pause" )
總結
以上是生活随笔 為你收集整理的Unity_Lua_语法基础 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。