我有以下有效的代码:
function group1color()
for _, roomid in ipairs(group1rooms) do
if roomid == '1+' then
Controls.room1background.Color = '#dba643'
end
if roomid == '2+' then
Controls.room2background.Color = '#dba643'
end
if roomid == '3+' then
Controls.room3background.Color = '#dba643'
end
end
end
但是,我需要再复制 roomid if 语句 5 次,然后为八个不同的房间组复制该函数 8 次。我想弄清楚是否有办法使用pairs或ipairs来简化这个?我主要是在寻找有关如何简化函数中的迭代的建议。
您可以通过以下方式访问全局变量_G["varname"]
function groupcolor()
for N = 1, 8 do
local groupNrooms = _G["group"..N.."rooms"]
for _, roomid in ipairs(groupNrooms) do
local M = roomid:match"^(%d+)%+$"
if M then
Controls["room"..M.."background"].Color = '#dba643'
end
end
end
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句