file:///D:/download/lua-5.3.5/lua-5.3.5/doc/readme.html
cygwin安装make gcc
make mingw
table.nums
math.modf # 无法保证小数部分的精确度,如math.modf(279.001), 小数部分不一定是0.001
分号结尾不是必须的,只要后面没有句子即可。
1. 使用math.modf(x/y)此函数,第一参数返回整数部分,第二个参数返回小数部分:
t1,t2 = math.modf(30/10)
2. 使用math.floor(x),返回不大于x的最大整数
a%b = a - math.floor(a/b)*b
local testTable = {
[1] = 0,
[0] = 1,
}
print("testTable=",#testTable)
testTable=1
local testTable = {
[1] = 0,
[2] = 1,
}
print("testTable=",#testTable)
testTable=2Ish
自动内存管理,lua运行一个垃圾收集器
来收集所有死对象
.实现了一个增量标记-扫描收集器.使用这两个数字来控制垃圾收集循环:垃圾收集器间歇率和垃圾收集器步进倍率。这两个数字都用百分数为单位(值150代表1.5倍)
垃圾收集器间歇率:控制收集器需要在开启新的循环前要等待多久。 <100时,收集器开启新的循环前不会有等待. x >=100时,收集器等到总内存使用量达到之前的x/100倍时,才开始新的循环.
垃圾收集器步进倍率:控制收集器运作速度相对于内存分配速度的倍率。
__call
function print_r ( t )
local print_r_cache={}
local function sub_print_r(t,indent)
if (print_r_cache[tostring(t)]) then
print(indent.."*"..tostring(t))
else
print_r_cache[tostring(t)]=true
if (type(t)=="table") then
for pos,val in pairs(t) do
if (type(val)=="table") then
print(indent.."["..pos.."] => "..tostring(t).." {")
sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
print(indent..string.rep(" ",string.len(pos)+6).."}")
elseif (type(val)=="string") then
print(indent.."["..pos..'] => "'..val..'"')
else
print(indent.."["..pos.."] => "..tostring(val))
end
end
else
print(indent..tostring(t))
end
end
end
if (type(t)=="table") then
print(tostring(t).." {")
sub_print_r(t," ")
print("}")
else
sub_print_r(t," ")
end
print()
end
pairs遍历table中的所有的key-vale 而ipairs会根据key的数值从1开始加1递增遍历对应的table[i]值
a = {[1] = "a1", [2] = "a2", [3] = "a3", [5] = "a4", [6] = "a5",}
for key, value in ipairs(a) do
print(key, value)
end
结果:
1 a1
2 a2
3 a3
a = {[1] = "a1", [2] = "a2", [3] = "a3", [5] = "a4", [6] = "a5",}
for key, value in pairs(a) do
print(key, value)
end
结果:
6 a5
2 a2
3 a3
1 a1
5 a4
定义
local table1 = {}
才能使用
table1[1] = "abc";
CacheManager._getInfo = {
[1] =
{
name = CacheManager._getName1,
desc = CacheManager._getDesc1
},
[2] =
{
name = CacheManager._getName2,
desc = CacheManager._getDesc2
},
}
function CacheManager:getDesc(type, id)
return CacheManager._getInfo[type].desc(self, id)
end
function CacheManager:getName(type, id)
return CacheManager._getInfo[type].name(self, id)
end
-- invoke
CacheManager c = CacheManager.new()
c:getName(2)
c:getDesc(1)
a and b or c
这里有个问题,就是当b是nil的时候会返回c的值
(a and {b} or {c})[1]
local key = 1
local switch = {
[1] = function()
print("switch:"..1)
end,
[2] = function()
print("switch:"..2)
end,
["test"] = function()
print("switch:test")
end,
}
local fSwitch = switch[key] --switch func
if fSwitch then --key exists
local result = fSwitch() --do func
else
--key not found
end
local CHS_NUMBER = {
[0] = "零",
[1] = "一",
[2] = "二",
[3] = "三",
[4] = "四",
[5] = "五",
[6] = "六",
[7] = "七",
[8] = "八",
[9] = "九",
[10] = "十",
[11] = "百",
[12] = "千",
[13] = "万",
}
-- 最大支持 < 10 0000
function getCHSNumberString(number)
local bitIdx = 1
local str = ""
while number > 0 do
local red = number%10
number = math.floor(number/10)
local zero = self:getString(self.CHS_NUMBER[0])
if red == 0 then
if not string.find(str, zero) and str ~= "" then
str = zero..str
end
else
if bitIdx > 1 then
str = self:getString(self.CHS_NUMBER[bitIdx + 8])..str
end
if number > 0 or bitIdx ~= 2 or red ~= 1 then
str = self:getString(self.CHS_NUMBER[red])..str
end
end
bitIdx = bitIdx + 1
end
return str
end
function getUTF8Len(str)
local len = string.len(str)
local left = len
local cnt = 0
local arr = {0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc}
while left ~= 0 do
local tmp = string.byte(str, -left)
local i = #arr
while arr[i] do
if tmp >= arr[i] then
left = left - i
break
end
i = i - 1
end
cnt = cnt + 1
end
return cnt
end
function isTableEmpty(t)
if not t then
return true
end
return next(t) == nil
end
使用package.loaded
和require
function S:reload()
local loadedTable = package.loaded
for k,v in pairs(loadedTable) do
local subStr = string.sub(k, 1, 4)
if subStr == "app." then
loadedTable[k] = nil
end
end
for k,v in pairs(globalTable) do
if not string.find(k, "Manager") and not string.find(k, "getClass") and not globalTable[k].init then
globalTable[k] = nil
end
end
end
local args = {func(self, params)}
local res = false
if args[1] == result_code then
res = true
end
return res, unpack(args)
local totalCount = 5
local strLen = 12
local strs = "abcdefghijklmnopqrstuvwxyz"
local array = {}
for curCount = 1, totalCount do
local tmpStrArray = {}
for i = 1, strLen do
local randValue = math.random(1, #strs)
tmpStrArray[#tmpStrArray + 1] = string.sub(strs, randValue, randValue)
end
local str = table.concat(tmpStrArray,'')
array[str] = str
end
Output:
jfbprlvargtw,
etlkdnmmcvwo,
ncjsadcwciqq,
mjulafvktjys,
muktlsfjctao,
local res
local rate
local tmpS
self._description1 = string.gsub(row.description1, "%[(.-)%]", function (s)
tmpS = {}
string.gsub(s, "(%d+)", function(s2)
tmpS[#tmpS + 1] = s2
end)
res = skillContent[tonumber(tmpS[1])]
rate = tmpS[2] and tonumber(tmpS[2])
if rate and rate > 0 then
res = res / rate
end
return res
end)
tbl["key1"] = true
for k, v in pairs(tbl) do
tbl["key2"] = nil
end
改为
tbl["key1"] = true
for k, v in pairs(tbl) do
if tbl["key2"] then
tbl["key2"] = nil
end
end
table.sort(list, function(a, b)
return true -- 改为 return false即可。
end)
function split(input, delimiter)
input = tostring(input)
delimiter = tostring(delimiter)
if (delimiter=='') then return false end
local pos,arr = 0, {}
-- for each divider found
for st,sp in function() return string.find(input, delimiter, pos, true) end do
table.insert(arr, string.sub(input, pos, st - 1))
pos = sp + 1
end
table.insert(arr, string.sub(input, pos))
return arr
end
lua中的string.gsub第二个参数可以使用匹配模式,并且模式串里面带中括号。那么表示会“捕捉”括号括起来的模式匹配到的字符串。
url = url .. "&extra4=" .. string.urlencode(self._attachURL)
function shuffle(t)
local tblLen = #t
while(tblLen > 0)
do
local idx = math.random(tblLen)
t[tblLen], t[idx] = t[idx], t[tblLen]
tblLen = tblLen - 1
end
return t
end
local testTbl = {1, 2, 3, 4, 5, 6, 7, 8, 9}
local retTbl = shuffle(testTbl)
for k, v in ipairs(retTbl) do
print(k, v)
end
math.floor(34.5/0.75)可能会产生两种结果
math.floor(34.5*1.34) = 46
math.floor(34.5*1.33) = 45
1/0.75的结果是不确定的1.33333334或者1.3333333
local per = -9000
local v = 30
math.floor(v * (1 + per/10000)) = 2
万分比运算:
math.floor(v * (10000 + per)/10000) = 3
xpcall(function(a, b)
end, function(err, level)
event_err_handle(err)
end, a, b)
asset执行了之后的代码是不会执行的
每帧执行如下语句内存会不断分配
local t = {3, 4}
ClassType = {
class = 1,
instance = 2,
}
function class(classname, super)
assert(type(classname) == "string" and #classname > 0)
local cls
if super then
cls = {}
setmetatable(cls, {__index = super})
cls.super = super
else
cls = {ctor = function() end, dtor = false}
end
cls.__cname = classname
cls.__ctype = ClassType.class
cls.__index = cls
cls.new = function(...)
local obj = setmetatable({}, cls)
obj.__cls = cls
obj.__ctype = ClassType.instance
obj:ctor(...)
return obj
end
cls.delete = function(self)
local curSuper = self.__cls
while curSuper ~= nil do
local dtor = rawget(curSuper, "dtor")
if dtor then
dtor(self)
end
curSuper = curSuper.super
end
end
return cls
end
一个坑:
-- TestClass.lua
local TestClass = class("TestClass")
TestClass.Tbl = {}
return TestClass
local t1 = TestClass.new()
t1.Tbl[#t1.Tbl + 1] = 3
local t2 = TestClass.new()
t2.Tbl[#t2.Tbl + 1] = 4
此时 #t2.Tbl == 2
检测全局变量:
if device.isStandalone then
local __global_mt = {
__newindex = function(t, k, v)
if not _G[k] then
print(debug.traceback("attempt to write an new global variable=" .. k, 2))
end
rawset(t, k, v)
end
}
setmetatable(_G, __global_mt)
end
= self
赋值操作获取部分变量出问题function WorldBaseActor:ctor()
WorldCachingSingleton.WorldActorMap[self:getActorIdx()] = self
end
ctor会修改元表,执行完子类和父类的ctor,self的指针已经修改
currentTime 总时间
self 总时间
_ENV
More0
-0
处理Moremath.atan(-1.0, -0.0)
math.atan(-1.0, 0.0)
得到两个值
0计算的精度问题导致0存在正负号,解决方式:
math.atan(x + 0, y + 0)