跳转到内容

Lua原生API

以下是编程元件中可以使用的 Lua 原生API,部分系统级操作的API无法使用(如os, io等)。 元件脚本中还提供了许多实用API,详见 官方提供API介绍

table相关

--遍历表
for key, value in pairs(table) do
...
end
--table函数
table.insert(table,pos, value)--在表指定位置添加元素。
table.insert(table, value)--在表末尾添加元素。
table.remove(table,pos)--删除表中指定位置的元素。
table.remove(table)--删除表中末尾的元素。
table.sort(table)--对表从小到大排序(表中不能有nil)。
table.concat(table, sep, start, end)--以分隔符分割字符串(除table外, 其他的参数都不是必填的, 分隔符的默认值是空字符, start的默认值是1, end的默认值是table的长度)。
table.maxn(table)--返回指定table中所有正数key值中最大的key值。如果不存在key值为正数的元素, 则返回0。
table.getn(table)--返回table中元素的个数。
table.pack(table)--返回table中的元素及长度。
table.unpack(table, start, end)--返回table里的元素。
--备注:我们也提供了Array系列函数,同样可以对table进行快捷操作,例如:
Array:ClearAllTheValue(array,value) --将表中指定值设为空
Array:DeleteAllTheValue(array,value) --删除表中指定值
Array:Sort(array,Array.SortType) --对数值组进行 升序("ascend")/ 降序:("descend")/乱序("disorder")排序
Array:IsContainValue(array,value) --表是否存在指定值
Array:IsContainIndex(array,value) --表是否存在指定编号
Array:GetIndexByValue(array,value) --获取表中指定值编号
Array:GetValueByRandomIndex(array) --获取表中随机值
Array:GetArrayLength(array) --获取表长度
Array:GetTheValueCount(array,value) --获取表中指定值数量

math相关

--math函数
math.abs(num)--取绝对值,返回num的绝对值。
math.atan2(x, y)--x/y的反正切值,返回x/y的反正切值,其中x是以弧度为单位的。
math.ceil(x)--向上取整,返回不小于x的最大整数。
math.floor(x)--向下取整,返回不大于x的最大整数。
math.deg(math.pi)--弧度转角度,返回以度为单位的角度x,其中x是以弧度为单位的。
math.rad(angle)--角度转弧度,返回以弧度为单位的角度,其中x是以度为单位的。
math.exp(x)--e幂x,返回以e为底x次方值。
math.mod(num1, num2)--除法取余,返回 x 除以 y 的余数。
math.frexp(val)--把双精度数val分解为数字部分(尾数)和以2为底的指数n,即val=x*2n。
math.ldexp(x, n)--返回x乘以2的n次方。
math.log10(x)--返回x的以10为基数的对数。
math.log(x)--返回x的自然对数。
math.max(n1,n2...n)--取参数最大值,返回参数中的最大值。
math.min(n1,n2...n)--取参数最小值,返回参数中的最小值。
math.modf(x)--把x分为整数部分和小数部分,会返回两个数字。
math.pi--获取圆周率,返回π的值。
math.pow(x, y)--返回x的y次幂。
math.random(x, y)--取随机数,返回x到y之间的随机数。
math.random(x)--取随机数,返回1到x之间的随机数。
math.randomseed(x)--将x设置为伪随机生成器的"种子",通常填入os.time()用时间作为种子。注意:用时间赋予的种子会导致同一时间生成的随机数相同;在循环中频繁调用会影响随机数的质量和性能,所以一般只在开始时调用-次。
math.sqrt(x)--开平方函数,x应为一个正数。
--三角函数相关
math.sin(x)--正弦函数,返回x的正弦值,其中x是以弧度为单位的。
math.cos(x)--余弦函数,返回x的余弦值,其中x是以弧度为单位的。
math.tan(x)--正切函数,返回x的正切值,其中x是以弧度为单位的。
math.acos(x)--反余弦函数,返回x的反余弦值,其中x是以弧度为单位的。
math.asin(x)--反正弦函数,返回x的反正弦值,其中x是以弧度为单位的。
math.atan(x)--反正切函数,返回x的反正切值,其中x是以弧度为单位的。
math.sinh(x)--双曲线正弦函数,返回x的双曲正弦值。
math.cosh(x)--双曲线余弦函数,返回x的双曲余弦值。
math.tanh(x)--双曲线正切函数,返回x的双曲正切值。
--备注:我们也提供了UMath系列函数,同样可以对计算进行快捷操作,例如:
UMath:GetVectorOperationResult(Engine.Vector,operation,Engine.Vector)--三维向量计算
UMath:GetVectorLength(Engine.Vector)--获取向量长度
UMath:GetNormalize(Engine.Vector)--获取向量归一化结果
UMath:GetDistance(Engine.Vector,Engine.Vector)--获取两点间的距离
UMath:GetPointToPoint(Engine.Vector,Engine.Vector)--获取两点间的方向
UMath:GetVectorRotate(Engine.Vector,axis,angle)--获取方向旋转指定角度后的方向
UMath:GetVectorAngle(Engine.Vector,Engine.Vector)--获取方向之间的夹角
UMath:GetVectorCross(Engine.Vector,Engine.Vector)--计算向量的叉乘
UMath:GetVectorDot(Engine.Vector,Engine.Vector)--计算向量的点乘
UMath:GetPosOffset(Engine.Vector,offsetx,offsety,offsetz)--获取位置偏移后的位置
UMath:GetHorizonAngle(Engine.Vector,Engine.Vector)--获取两点间的水平夹角
UMath:GetVerticleAngle(Engine.Vector,Engine.Vector)--获取两点间的垂直夹角
UMath:GetVectorProjection(Engine.Vector,Engine.Vector)--计算向量的投影
UMath:GetSequence(left,right,step)--获取数值序列
UMath:GetLerp(object1,object2,weight)--获取对象插值
UMath:EulerToQuaternion(Engine.Vector)--将欧拉角转为四元数
UMath:QuaternionToEuler(Engine.Vector)--将四元数转为欧拉角

其他

以下这些都是可用的

--math (数学计算库)
--绝对值计算
print(math.abs(-15)) -- 15
--向上取整
print(math.ceil(5.2)) -- 6
--string (字符串处理库)
--格式化输出
print(string.format("时间: %02d:%02d",9,5)) -- 时间: 09:05
--查找子串
print(string.find("Hello World","World")) -- 7 11
--table (表格操作库)
--插入元素
local tbl = {1,2}
table.insert(tbl,3) -- {1,2,3}
table.insert(tbl,2,1.5) -- {1,1.5,2,3}
--assert (断言验证)
function CreatePlayer(name)
assert(type(name)=="string", "玩家名称必须为字符串") -- 类型不符时抛出错误
end
--ipairs (顺序遍历)
--遍历表部分(遇到nil停止)
for i,v in ipairs({"苹果",nil,"香蕉"}) do
print(i,v) -- 日志打印 1:苹果
end
--pairs (全表遍历)
--遍历所有键值对(遇到nil不会停止)
for k,v in pairs({name='Lua',ver=5.1}) do
print(k,v) -- 日志打印name:Lua 和 ver:5.1
end
--next
--获取表格下一对键值
local t = {a=1,b=2}
print(next(t)) -- a 1
print(next(t, "a")) -- b 2
--pcall (保护调用)
--捕获运行时错误
local ok,err = pcall(function()
print(1 + "a") -- 类型错误
end)
print(ok,err) -- false 错误信息
--xpcall (带错误处理的保护调用)
--自定义错误处理
xpcall(function()
error("测试错误")
end, function(err)
print("错误捕获:", err) -- 日志打印错误信息
end)
--raw*系列 (原始访问)
--绕过元方法直接操作
local tab = setmetatable({}, {__index = function() return "meta" end})---创建元表
getmetatable(tab) --获取tab的元表
print(tab.key) -- meta(触发元方法)
print(rawget(tab,"key")) -- nil(直接访问)
print(rawequal(1,"1")) -- false(严格比较)
rawset(tab,"key",100) -- 直接赋值
print(rawlen("abcd")) -- 4(字符串长度)
print(rawlen({1,2,nil,4})) -- 4(表长度)
--select (参数选择)
--获取可变参数
function test(...)
print(select("#",...)) -- 参数数量
print(select(2,...)) -- 从第2个开始的所有参数
end
test("A","B","C") -- 日志打印3 → B C
--tonumber/tostring (类型转换)
print(tonumber("3.14")) -- 3.14(数字)
print(tostring(1234)) -- "1234"(字符串)
--type (类型检测)
print(type({})) -- table
print(type(print)) -- function
--_VERSION (版本信息)
print(_VERSION) -- 日志打印"Lua 5.1"(根据实际环境)