I've created a form to choose the settings. These settings are automatically saved from the client in this form:
Lua Code:
["Setting"] = {
["track"] = "Water Shield",
["duration"] = {
["minimum"] = {
["enabled"] = 1,
["value"] = 50,
},
["maximum"] = {
},
},
["stack"] = {
["minimum"] = {
},
["maximum"] = {
["enabled"] = 1,
["value"] = 2,
},
},
}
I created this function to save the data, when the form is closed or the apply button is clicked:
Lua Code:
function loadSettings()
local options = { 'duration', 'stack' }
for i=1, #options do
local option = options[i]
if not db[option] then
db[option] = { minimum = {}, maximum = {} }
end
local enabled
local value
enabled = db[option].minimum.enabled
if (enabled) then _G["min"..option].cbutton:SetChecked(1) else _G["min"..option].cbutton:SetChecked(0) end
value = db[option].minimum.value
if (value) then _G["min"..option].ebox:SetText(value) else _G["min"..option].ebox:SetText("\00") end
enabled = db[option].maximum.enabled
if (enabled) then _G["max"..option].cbutton:SetChecked(1) else _G["max"..option].cbutton:SetChecked(0) end
value = db[option].maximum.value
if (value) then _G["max"..option].ebox:SetText(value) else _G["max"..option].ebox:SetText("\00") end
end
end
Where
• db = The "Setting" table
• SetChecked(enable) = True to check the button; false to uncheck (boolean)
• SetText("text") = Text to be placed in the edit box (string)
Could you help me to make the code less repetitive and more clean and elegant? Expecially from 13 to 22 line, I wish to make this lines more DRY (Don't Repeat Yourself)