16.自带库

16.自带库


16.1 知识点

时间

os.time() 系统时间戳

print(os.time()) -- 1695818702

os.time() 返回当前时间对应的时间戳。实际结果会随着运行时间变化,示例里的值不用纠结,只要知道它返回的是一个数字即可。

os.time({ year = 年, month = 月, day = 日 }) 传入时间相关的参数得到时间戳

print(os.time({ year = 2014, month = 8, day = 14 })) -- 1407988800

这种写法可以把指定日期转换成时间戳。项目里如果要做活动时间、签到时间、过期时间之类的判断,经常会用到类似思路。

os.date("*t") 返回当前时间的表对象

local nowTime = os.date("*t")

for k, v in pairs(nowTime) do
    print(k, v)
    -- hour	20
    -- min	45
    -- wday	4
    -- day	27
    -- month	9
    -- year	2023
    -- sec	2
    -- yday	270
    -- isdst	false
end

os.date("*t") 返回的是一张表。
这样拿年月日时分秒会更清晰,比直接处理时间戳方便一些。

数学运算

math.abs(数值) 绝对值

print(math.abs(-1)) -- 1

math.deg(数值) 传入弧度转角度

print(math.deg(math.pi)) -- 180

math.cos(数值) 三角函数 传弧度

print(math.cos(math.pi)) -- -1

math.floor(数值) 向下取整

print(math.floor(2.6)) -- 2

math.ceil(数值) 向上取整

print(math.ceil(5.2)) -- 6

math.max(数值1, 数值2) 最大值

print(math.max(1, 2)) -- 2

math.min(数值1, 数值2) 最小值

print(math.min(4, 5)) -- 4

math.modf(数值) 把数值分离成整数部分和小数部分并返回

print(math.modf(1.2)) -- 1	0.2

math.pow(底数, 幂) 幂运算

print(math.pow(2, 5)) -- 32

Lua 里也可以直接用 ^ 做幂运算:

print(2 ^ 5) -- 32

math.randomseed(任何数值) 设置随机数种子

math.randomseed(os.time())

math.random(最大值) 返回随机数

print(math.random(100))
print(math.random(100))

随机数最好先设置随机数种子。
如果每次都用相同的种子,第一次随机出来的结果也会相同。在做抽奖、随机怪物、随机掉落时注意。

math.sqrt(底数) 开方

print(math.sqrt(4)) -- 2

字符串库

常用函数在 string 表里。

string.upper(字符串) 小写转大写

print(string.upper("abc")) -- ABC

string.lower(字符串) 大写转小写

print(string.lower("ABC")) -- abc

string.sub(字符串, 起始索引, 结束索引) 截取字符串

print(string.sub("abcdef", 2, 4)) -- bcd

string.find(字符串, 查找内容) 查找字符串

print(string.find("abcdef", "cd")) -- 3	4

string.find 默认会按 Lua 的匹配规则处理。遇到 .、+、% 这类特殊字符时再注意转义或使用纯文本查找。

string.format(格式字符串, 参数...) 格式化字符串

print(string.format("name:%s age:%d", "Tom", 18)) -- name:Tom age:18

表库

table.insert(表, 元素) 插入元素

local t = { 1, 2, 3 }

table.insert(t, 4)

print(t[4]) -- 4

table.remove(表, 索引) 删除元素

local t = { 1, 2, 3 }

table.remove(t, 2)

print(t[1]) -- 1
print(t[2]) -- 3

table.sort(表, 排序函数) 排序

local t = { 3, 1, 2 }

table.sort(t)

for i, v in ipairs(t) do
    print(i, v)
    -- 1	1
    -- 2	2
    -- 3	3
end

table.concat(表, 分隔符) 拼接数组字符串

local t = { "123", "456", "789" }

print(table.concat(t, "@@")) -- 123@@456@@789

输入输出库

io 库主要用来做文件读写。
了解最简单的打开、写入、读取、关闭即可。

io.open(路径, 模式) 打开文件

local file = io.open("test.txt", "w")

常见模式:

  • "r":只读。
  • "w":写入,会覆盖原文件。
  • "a":追加写入。
  • "r+":读写,文件需要存在。
  • "w+":读写,会覆盖原文件。

写入文件

local file = io.open("test.txt", "w")

if file then
    file:write("hello lua")
    file:close()
end

读取文件

local file = io.open("test.txt", "r")

if file then
    local content = file:read("*a")
    print(content)
    file:close()
end

读写文件要看路径、权限、平台差异(Windows / 移动端路径不一样)
比如 Unity 项目里通常不会随便往工程目录写文件,而是用项目规定的可写路径。

路径

package.path Lua脚本加载路径

print(package.path)                    -- ;.\?.lua;C:\Program Files (x86)\Lua\5.1\lua\?.lua;C:\Program Files (x86)\Lua\5.1\lua\?\init.lua;C:\Program Files (x86)\Lua\5.1\?.lua;C:\Program Files (x86)\Lua\5.1\?\init.lua;C:\Program Files (x86)\Lua\5.1\lua\?.luac

package.path = package.path .. ";C:\\" -- 可以进行拼接,Lua 字符串里 \ 需要写成 \\

print(package.path)                    -- ;.\?.lua;C:\Program Files (x86)\Lua\5.1\lua\?.lua;C:\Program Files (x86)\Lua\5.1\lua\?\init.lua;C:\Program Files (x86)\Lua\5.1\?.lua;C:\Program Files (x86)\Lua\5.1\?\init.lua;C:\Program Files (x86)\Lua\5.1\lua\?.luac;C:\

package.pathrequire 有关系。
简单理解就是:require 找脚本时,会参考 package.path 里的搜索规则。
项目里这套路径规则可能会被框架或者自定义 Loader 接管,尤其是 Unity Lua 热更项目,不一定直接按原生 Lua 的文件路径规则。

debug库简单说明

Lua 有 debug 库,debug.getinfodebug.getupvaluedebug.sethook 偏调试和性能分析,不展开。


16.2 知识点代码

Lesson16_自带库.lua

print("**********自带库************")

print("**********知识点一 时间************")

-- os.time() 得到系统时间戳
print(os.time()) -- 1695818702

-- os.time({ year = 年, month = 月, day = 日 }) 传入时间相关的参数得到时间戳
print(os.time({ year = 2014, month = 8, day = 14 })) -- 1407988800

-- os.date("*t") 会返回当前时间的表对象
local nowTime = os.date("*t")

for k, v in pairs(nowTime) do
    print(k, v)
    -- hour	20
    -- min	45
    -- wday	4
    -- day	27
    -- month	9
    -- year	2023
    -- sec	2
    -- yday	270
    -- isdst	false
end

print("**********知识点二 数学运算************")

-- math.abs(数值) 绝对值
print(math.abs(-1)) -- 1

-- math.deg(数值) 传入弧度转角度
print(math.deg(math.pi)) -- 180

-- math.cos(数值) 三角函数,传弧度
print(math.cos(math.pi)) -- -1

-- math.floor(数值) 向下取整
print(math.floor(2.6)) -- 2

-- math.ceil(数值) 向上取整
print(math.ceil(5.2)) -- 6

-- math.max(数值1, 数值2) 最大值
print(math.max(1, 2)) -- 2

-- math.min(数值1, 数值2) 最小值
print(math.min(4, 5)) -- 4

-- math.modf(数值) 把数值分离成整数部分和小数部分并返回
print(math.modf(1.2)) -- 1	0.2

-- math.pow(底数, 幂) 幂运算
print(math.pow(2, 5)) -- 32

-- Lua 中也可以直接使用 ^ 做幂运算
print(2 ^ 5) -- 32

-- math.randomseed(任何数值) 设置随机数种子
math.randomseed(os.time())

-- math.random(最大值) 返回随机数
-- 随机前最好设置随机数种子
-- 相同的随机数种子,每次运行第一次随机出来的结果也会相同
print(math.random(100))
print(math.random(100))

-- math.sqrt(底数) 开方
print(math.sqrt(4)) -- 2

print("**********知识点三 字符串库************")

-- 字符串库前面已经单独讲过,这里只做简单回顾
-- 常用函数都放在 string 表里

print(string.upper("abc")) -- ABC
print(string.lower("ABC")) -- abc
print(string.sub("abcdef", 2, 4)) -- bcd
print(string.find("abcdef", "cd")) -- 3	4
print(string.format("name:%s age:%d", "Tom", 18)) -- name:Tom age:18

print("**********知识点四 表库************")

-- 表库前面也已经单独讲过,这里只列几个常用函数

local t = { 1, 2, 3 }

table.insert(t, 4)
print(t[4]) -- 4

table.remove(t, 2)
print(t[1]) -- 1
print(t[2]) -- 3

local t2 = { 3, 1, 2 }

table.sort(t2)

for i, v in ipairs(t2) do
    print(i, v)
    -- 1	1
    -- 2	2
    -- 3	3
end

local t3 = { "123", "456", "789" }

print(table.concat(t3, "@@")) -- 123@@456@@789

print("**********知识点五 输入输出库************")

-- io 库主要用来做文件读写
-- 最简单的打开、写入、读取、关闭即可

local file = io.open("test.txt", "w")

if file then
    file:write("hello lua")
    file:close()
end

file = io.open("test.txt", "r")

if file then
    local content = file:read("*a")
    print(content)
    file:close()
end

print("**********知识点六 路径************")

-- package.path Lua 脚本加载路径
print(package.path)                    -- ;.\?.lua;C:\Program Files (x86)\Lua\5.1\lua\?.lua;C:\Program Files (x86)\Lua\5.1\lua\?\init.lua;C:\Program Files (x86)\Lua\5.1\?.lua;C:\Program Files (x86)\Lua\5.1\?\init.lua;C:\Program Files (x86)\Lua\5.1\lua\?.luac

package.path = package.path .. ";C:\\" -- 可以进行拼接

print(package.path)                    -- ;.\?.lua;C:\Program Files (x86)\Lua\5.1\lua\?.lua;C:\Program Files (x86)\Lua\5.1\lua\?\init.lua;C:\Program Files (x86)\Lua\5.1\?.lua;C:\Program Files (x86)\Lua\5.1\?\init.lua;C:\Program Files (x86)\Lua\5.1\lua\?.luac;C:\

print("**********知识点七 debug库简单说明************")

-- Lua 也有 debug 库,不过这篇不展开
-- debug.getinfo、debug.getupvalue、debug.sethook 这些接口更偏调试、性能分析、闭包和 Upvalue 排查


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏