将多个 Lua 参数合并成表深度
2013-10-29 4:37:12
收藏:0
阅读:107
评论:2
我目前有这段代码,但我觉得这可以更好地优化:
local config = {
val = 1,
background = {
foo = 5,
textures = {
bar = 'okay',
layers = {
alpha = 1,
color = {
red = 255,
blue = 0,
green = 100
}
}
}
},
sub = {
val = 5,
foo = {
val = true,
bar = {
}
}
}
}
local function set_config(...)
local arg = {...}
if type(arg[1]) == 'table' then
table.extend(config, arg[1])
elseif #arg == 2 then
if type(arg[2]) == 'table' then
table.extend(config[arg[1]], arg[2])
else
config[arg[1]] = arg[2]
end
elseif #arg == 3 then
if type(arg[3]) == 'table' then
table.extend(config[arg[1]][arg[2]], arg[3])
else
config[arg[1]][arg[2]] = arg[3]
end
-- 这是第 4 次。重复性越来越高
elseif #arg == 4 then
if type(arg[4]) == 'table' then
table.extend(config[arg[1]][arg[2]][arg[3]], arg[4])
else
config[arg[1]][arg[2]][arg[3]] = arg[4]
end
-- 为了我想要实现的内容,我应该一直使用这种 elseif 直到无限
end
end
-- 我可以尽可能地深入表中
现在我可以这样做:
set_config('val', 3)
set_config('sub', {
foo = false
})
但我想深入表格而没有现在的 set_config 的限制
所以我可以这样做:
set_config('background', 'textures', 'layers', 'medium', 'color', 'red', 255)
为了澄清,table.extend 如下:
function table.extend(t1, t2)
for k, v in pairs(t2) do
if (type(v) == "table") and (type(t1[k] or false) == "table") then
table.extend(t1[k], t2[k])
else
t1[k] = v
end
end
return t1
end
我尝试了一些东西,但我的 Lua 知识相当有限。
lhf 的答案几乎正确,这是我想要的结果:
local function set_config(...)
local a={...}
local n=#a
local t=config
if #a == 1 and type(a[1]) == 'table' then
table.extend(config, a[1])
return
end
for i=1,n-2 do
local k=a[i]
t[k]=t[k] or {}
t=t[k]
end
if type(t[a[n-2]]) == 'table' then
table.extend(t[a[n-1]], a[n])
else
t[a[n-1]] = a[n]
end
end
点赞
用户1244588
使用一些元表的魔术,你可以完全摆脱set_config:
local config, config_mt
config_mt = {
__index = function(t, k)
t[k] = setmetatable({}, config_mt)
return t[k]
end,
}
config = setmetatable({}, config_mt)
config.background.textures.layers.medium.color.red = 255
print(config.background.textures.layers.medium.color.red)
不过,有一个缺点:从存储中恢复您的配置后,您必须遍历结构体以再次应用元表。
2013-10-30 18:30:31
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
试试这个:
local config={} function set_config(...) local a={...} local n=#a local t=config for i=1,n-2 do local k=a[i] t[k]=t[k] or {} t=t[k] end t[a[n-1]]=a[n] end set_config('background', 'textures', 'layers', 'medium', 'color', 'red', 255) print(config.background.textures.layers.medium.color.red)