Lua 是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开放, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。
lua 经常被用作 cocos-2dx 引擎开发2D安卓游戏
lua也被用作后端开发,现在很多后端开发都用的是纯粹的lua语言
lua也被用做[热更新(在unity里面直接做)][修改脚本]
lua和Python一样没有 ; 和 {}
Lua语言中的关键字
string nil boolean table function number thread userdata
table 有{}的就是table (表)
string 表示字符串 用""和’'表示
boolean 表示布尔值判断false和true
function 函数值
number 十六进制和科学计数法 表示浮点数
nil 空类型 相当于 false
userdata 脚本用户只能使用不能定义
local后面跟变量加常量表示这个 式子只在这块有效果
比如
function num ()
local i = 1
j = 2
end
--指的是在这个式子中 i只有在这里被赋值为1 而j称为全局变量 在整个程序中都有效
在Lua中 不等于 用的是~=而不是!=
‘%’ 取余 如果%取余==0 的时候意味着可以整除
‘and’ 两边都为真时,这个式子为真否则为假
‘or’ 两边只要有一个为true 这个式子都为true
在函数外部定义全局变量在函数内部再次定义同名称的全局变量会覆盖函数外部的
若进行变量的定义 想清楚使用的是那种 (在方法里和方法外的区别)
C#最多使用的是local
lua若有局部变量就使用局部的
在变量使用前必须赋初值,不然会报错 变量value为nil
在返回值的过程中遵循解构赋值
循环for
for循环里面步长不会动态改变
for i = 5, f(5),3 do --这里 3就是步长,可以不敲步长,不敲的话默认为C#中的i++
i = i +100
print(“aaaa”)
end
不管啥语言, 只要方法名称进行参数传递的都是理解为委托
-- 变量: 可以被修改的量是变量
-- 常量: 不能被修改的量是常量
-- 命名规则还是依据C#
-- i = 10
-- -- print( i+"20" )
-- -- 因为lua没有常量的概念,沿用python语法规则 假常量 NUMBER
-- USERNAME = "zhangsan"
-- print( USERNAME )
-- isDone = false
-- print( isDone )
-- i = 10 --全局
-- -- 全局变量和局部变量
-- function TestLocal( )
-- local i = 250
-- print( "i in TestLocal" )
-- i = nil --局部
-- print( i )
-- end
-- TestLocal()
-- print( "==============================" )
-- print( i ) -- 全局
-- 解构赋值
-- 在变量使用前必须赋初值
-- a, b = 10, 2*x
-- print( a )
-- print( b )
-- x = 10
-- c = 20
-- -- tmp = x
-- -- x = c
-- -- c = tmp
-- x ,c = c, x
-- print( x )
-- print( c )
-- a, b, c = 0
-- print( a, b,c )
-- function TestLocal( )
-- local i = 250
-- print( "i in TestLocal" )
-- return i ,50, 120
-- end
-- number01, number02 = TestLocal()
-- print( number01,number02 )
-- for i = 1, 10,2 do
-- print(i)
-- end
-- function f(x)
-- print("function")
-- return x*2
-- end
-- function Demo002(xx )
-- print( "xxx",xx )
-- return xx+1
-- end
-- tmp = 1
-- for i = 5, f(5),3 do
-- i = i +100
-- print("aaaa")
-- end
-- days = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}
-- for i,v in ipairs(days) do print(i) end
-- for i = 1, 9 do
-- for j = 1, 9 do
-- if( i <= j)
-- then
-- print( i*j )
-- end
-- end
-- end
--金字塔
-- sex = 'a'
-- if (sex == '男')
-- then
-- print( "男生" )
-- else
-- print( "女生" )
-- end
-- sex = '男'
-- if(sex == '男')
-- then
-- print( "男生" )
-- elseif (sex == '女') then
-- print( "女生" )
-- --do
-- else
-- print( "Error" )
-- end
-- if(sex == "男")
-- then print( "nansheng" )
-- end
-- if(sex == "女")
-- then print( "nvsheng" )
-- end
-- 分数
score = 60
if(score >= 60)then
if (score >= 80) then
print( "80" )
else
if(score >= 70) then
print( "70" )
else
print( "60" )end
end
end
--转小写 upper转大写
str01= "AAAA"
tmpStr = string.lower( str01 )
print( tmpStr )
--替换字符串中的关键字
tmpStr = "AABCAAAWFCAACDCCAA"
-- 都替换怎么办?
-- repStr = string.gsub( tmpStr, "AA", "QQ")
-- print( tmpStr )
-- print( repStr )
print( string.find( tmpStr, "AAA" ) ) --返回开始, 结束
--拷贝字符串 3指的是次数
str01 = "AACC"
tmpStr = string.rep( str01, 3 )
print( tmpStr )
%a+ 全是字母的元素值
for word in string.gmatch("1112a123v45s545645a44444", "%d+") do print(word) end
正则表达式
print( string.match("I have 2 questions for you.", "%a+ %d+ string.format("%d, %q", string.match("I have 2 questions for you.", "(%d+) (%a+)"))") )
d 数字 q 字符串 a+ 字母多个
print(string.format("%d, %q", string.match("I have 2 questions for you.", "(%d+) (%a+)")))
迭代器 gmatch 一般考虑for x v in strng.gmatch() do XXXX end
match 找到匹配项
find: 找到index
rep: 拷贝N次