游戏里面用lua来热更新的 redis的默认方法也是lua(我觉得是因为和nosql很搭)
nil
a = {[0]="123","32","23"};
_
---[[
可以注销掉块注释type
函数永远返回一个字符串false
和nil
视为假,将0
和空字符串视为真4
,0.4
,4.58e-3
,0.3e12
,5e+20
\<ddd>
表达数值转义,[[ ]]
,[===[ ]===]
界定换行字符串,(类似的界定注释)tonumber
, tostring
不成功返回nil
#a
获取字符串a的长度,table.maxn
对于nil更加安全a ={},b=a
b保持了一个对于a所指向table的引用a.x
和a["x"]
是一样的,点作为记录,key暗示任意字符串x - x%1
取整x,y=y,x
交换do end
构成基本块(local
)a = io.read("*number")print(a)
do return end
(...)
, 获取arg
goto
(用来编写状态机)sort
table.sort(tb, function(a,b) return (a.name > b.name) end
-- 对于and来说第一个操作数为真,返回第二个操作数-- or第一个操作数为假,返回第二个操作数(a and b) or c -- a ? b : c, b不可以为false
foo = function(x) return 2 * x end
需要先定义局部变量
local fact -- 这一句和下一句要求分开【bug主要在这里】fact = function(n)if n == 0 then return 1else return n * fact(n-1)endendorlocal function fact(n) if n==0 then return 1 else return n * fact(n-1) endend
内部的函数体可以访问外部函数的局部变量(upvalue)
domo 重定义限制程序打开文件
do local oldOpen = io.open io.open = function(filename, mode) if access_OK(filename, mode) then return oldOpen(filename, mode) else return nil, "access denied" end endend
-- g can use f herelocal f = function(...)endlocal g = function(...)f()end
需要先声明
local factfact = function(n) if n == 0 then return 1 else return n * fact(n-1) endend
a = {}; a.x = 1;b = {}; b.x = 1;a ~= ba.foo = function(x,y) return x + y endunpack(a)-- 分离一层
do local i = 1enddo return end -- 用于调试
if conditions thenelseif conditions thenendwhile condition doendrepeat statements;until conditions;
for var = beg,end,step do -- 如果为表达式一次性求职endfor i,v in ipairs(a)doendfor key in pairs(v)doend-- 死循环for i = 1,math.huge doend
for <var-list> in <exp-list> do end
for i,v in ipairs(a) do end
require
会搜索目录加载文件,避免同一个文件if n then error("123") endassert(io.read("*number"), "123")
if n then error("123") endassert(io.read("*number"), "123")
Lua将所有关于协同程序的函数放置在一个名为coroutine的table里面
fco = coroutine.create(f) -- 创建,处于挂起状态coroutine.resume(fco) -- 启动或者再次启动coroutine.status(co) -- suspended/running/dead/normalcoroutine.yield() -- 函数内部挂起,yield(1,2)将返回1,2
非抢占式多线程 管道,迭代器
fco = coroutine.create(f) -- 创建,处于挂起状态coroutine.resume(fco) -- 启动或者再次启动coroutine.status(co) -- suspended/running/dead/normalcoroutine.yield() -- 函数内部挂起,yield(1,2)将返回1,2
-- t.__tostring = xxx-- set:local mt = { __index = t}setmetatable(proxy, mt) __tostring -- 打印__index -- t[x] getter__newindex -- t[x] setter
_G[x]
-- 创建a = 1 -- create a global variable -- change current environment setfenv(1, {_G = _G}) _G.print(a) --> nil _G.print(_G.a) --> 1-- 继承a = 1 local newgt = {} -- create new environment setmetatable(newgt, {__index = _G}) setfenv(1, newgt) -- set it print(a) --> 1
function Account:withdraw(v) -- a:withdrawfunction withdraw(self, v) -- a.withdraw(可以互相通用)
__add
,__mul
,__sub
,__div
,__unm
,__mod
,__pow
,关系方法包括__eq
,__lt
,__le
__index(t,k)
方法,更新为__newindex(t,k,v)
方法
1 2 3 4 5 6 7 8 9 10 11function setDefault(t,d) local mt = {__index=function() return d end} setmetatable(t,mt) end local key = {} -- trick, 唯一索引 local mt = {__index = function(t) return t[key] end} fuction setDefault(t,d) t[key] = d setmetatable(t,mt) end function Account:withdraw(v) -- a:withdrawfunction withdraw(self, v) -- a.withdraw(可以互相通用)
weak
引用 metatable
的__mode
域来制定的,k
指key
是weak
,v
指value
是weak
1 2 3 4 5a, b = {}, {} setmetatable(a,b) b.__mode = \"k\" collectgarbage() 用途: 记忆函数,对象的属性关联
setfenv(f, table):设置一个函数的环境 (1)当第一个参数为一个函数时,表示设置该函数的环境 (2)当第一个参数为一个数字时,为1代表当前函数,2代表调用自己的函数,3代表调用自己的函数的函数,以此类推
数学库由算术函数的标准集合组成,比如三角函数库(sin, cos, tan, asin, acos, etc.), 幂指函数(exp, log, log10),舍入函数(floor, ceil)、max、min,加上一个变量 pi。数学 库也定义了一个幂操作符(^)。 所有的三角函数都在弧度单位下工作。(Lua4.0 以前在度数下工作。)你可以使用 deg 和 rad 函数在度和弧度之间转换。
math.random 用来产生伪随机数,有三种调用方式:
lua 假定array在最后一个非nil位置结束
table.getn -- memory searchtable.setntable.insert(a,x) -- pushtable.remove(a) -- poptable.insert(a,1,x), table.remove(a,1)table.sort
string.len(s)string.rep(s,n) -- repeat s for n timesstring.lower(s) -- upperstring.sub(s,i,j) -- begin with 1. end as -1i, j = string.find(s, "hello", [beg pos])s, count = string.gsub(ori, mod, replace[, time])
简单模式/完全模式
io.input(filename), io.output(filename)io.read() -- a lineio.read(*all)io.read(*number)io.read(*line)io.read(*num) -- char numio.read(0) -- whether EOFio.write("",math.sin(3),"")f = assert(io.open("xxx.xxx", "r"))local lines, rest = f:read(BUFISIZE,"*line")outf:flush
io.input(filename), io.output(filename)io.read() -- a lineio.read(*all)io.read(*number)io.read(*line)io.read(*num) -- char numio.read(0) -- whether EOFio.write("",math.sin(3),"")f = assert(io.open("xxx.xxx", "r"))local lines, rest = f:read(BUFISIZE,"*line")outf:flush
string.format(fmt.."%q", unpack(arg), "1234")table.sort(tb1, function(a,b) return (a.name>b.name) end)io.read(*all)
既然你看到了最后,不如看下传统热更新框架~~ 1 2 3 4 5 6xxxPanel.prefab, xxx.AssetBundle=>build Windows Resource GlobalGenerator:OnResourceInited->LuaScriptPanel->xxxPanel GameManager.Lua->OnInitOK()->CtrlManager.Init() Define.Lua:"xxx=xxxCtrl" GameManager.Lua:ctrl:Awake() LayerPanel
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有