If you want to try a more advanced (and "prettier", from a coding point of view) implementation, Lua metatables allow for something like this:
Code:
-- this is simply an example implementation that adds "s" to the seconds value passed and forwards to :SetText - you'd usually have your timer logic, setting up OnUpdate frames and/or animations, here
local setTimer = function(timer,seconds)
timer:SetText(("%ds"):format(seconds))
if seconds > 10 then
timer:SetTextColor(0,1,0)
else
timer:SetTextColor(1,0,0)
end
timer:Show()
end
-- create our myTimers table that will hold the timer objects
-- it is an empty table (arg1 to setmetatable) with a metatable containing an __index method (arg2 to setmetatable)
local myTimers = setmetatable({},{__index=function(t,key)
-- create fontstring
local timer = parent:CreateFontString()
-- anchoring
if key == 1 then
timer:SetPoint("TOP",parent,"BOTTOM",0,-5) -- anchor point for first timer
else
timer:SetPoint("TOP",t[key-1],"BOTTOM",0,-5) -- anchor to previous timer
end
-- add the method
timer.SetTimer = setTimer
timer:SetTextColor(1,0,0) -- default text color
timer:SetFont("Fonts\\FRIZQT__.TTF",13,"OUTLINE")
t[key] = timer
return timer
end})
This gives your myTimers table a metamethod that will automatically generate fontstrings as you access a key that doesn't exist yet. Example for accessing:
Code:
-- stuffToShow = {1, 15, 32}
-- show table entries in our fontstrings using the :SetTimer method we added to them
local nStuff = #stuffToShow
for i=1,nStuff do
myTimers[i]:SetTimer(stuffToShow[i]) -- we don't need to worry about whether this timer exists - it's automatically created by the metamethod if it doesn't!
end
-- hide excess timers
for i=(nStuff+1),#myTimers do
myTimers[i]:Hide()
end
Drycoded, but you get the idea.