本文主要是介绍lua配置表优化以及元表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
table1 =
{[100] = {Hung = 0,IDType = 7,InitMapID = 10000,Name = "荣誉阶梯",Timeout = 0},[101] = {Hung = 0,IDType = 0,InitMapID = 10100,MinLv = 50,Name = "盘山道",NumEveryDay = 1,PworldID = 101,Timeout = 1200,Type = 2}
}local __default_values = {Cheats = "",ClientCtrlFlag = 0,Des = "",DungeonEffect = 0,EnterUseVig = 0,FailBtnFlag = 7,GeneralWaveID = 0,Hung = 2,IDType = 26,InitMapID = 0,MaxLv = 0,MinLv = 0,Name = "守护者副本",NumEveryDay = 0,OpeningEffect_Delay = 0,OpeningEffect_Id = 0,OpeningEffect_Time = 0,PopAlive = 0,PworldID = 100,RelativeID = 0,ResourceID = "",StartPositon = 0,Timeout = 300,Type = 0,Typemain = 0,exit = 0,exit2 = 0,q_isscript = 0
}local mt = {__index = function(tbl, key)local val = rawget(tbl, key)if val then return valelse return __default_values[key]endend,__newindex = function(tbl, key, value)rawset(tbl, key, value)end,__pairs = function (tbl)local func = function (tbl, key)local nk, nv = next(__default_values, key)if nk then nv = tbl[nk]endreturn nk, nvendreturn func, tbl, nilend
}-- 设置元表
for _,item in pairs(table1) dosetmetatable(item, mt)
endGetList = function()--不用元表可以使用填充的方式--[[for _, tbl in pairs(table1) dofor key, value in pairs(__default_values) dolocal val = rawget(tbl, key)if val==nil thenrawset(tbl, key, value)endendend--]]return table1;
end;Get = function(id) if table1[id] == nil thenPrintConfig(string.format("<color=red>X_itemdef表id %s 的数据为空!</color>",id))endreturn table1[id];
end;for k, tbl in pairs(GetList()) dofor m, n in pairs(tbl) doprint(k, m, n)end
end
这篇关于lua配置表优化以及元表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!