Please give meaningful names to your variables. Variable names such as
f.h and
f.h.v are bordering on obfuscation, which is a violation on Blizzard's Addon Policy.
2) Add-on code must be completely visible.
The programming code of an add-on must in no way be hidden or obfuscated, and must be freely accessible to and viewable by the general public.
|
Code:
f.h = select(1, select(1, f:GetChildren()):GetChildren())
On another note, you should avoid making
select(1,...) calls. It just wastes CPU cycles on a meaningless function call and does absolutely nothing.
With these and a few other optimizations in mind, here's what I could work out.
Lua Code:
-- This table will link the StatusBars to our FontStrings
-- This is to keep the nameplate tables clean
local TextObjects={};
-- This processes the WorldFrame's children and populates our lookup table
local function ProcessWorldFrameChildren(...)
-- Scan through the children listed in our args
-- Dynamic indexing of arguments is the only practical use of select()
for i=1,select("#",...) do
local nameplate=select(i,...);
-- When we refer to a return from a function, call once, cache the value in a local, and check the local instead of using redundant calls
local name=nameplate:GetName();
if name and name:find("^NamePlate%d+$") then
-- No use of select() here, only the first return of these function calls are used anyway
local statusbar=nameplate:GetChildren():GetChildren();
-- If we don't have a FontString for this StatusBar, we create one
if not TextObjects[statusbar] then
local text=statusbar:CreateFontString(nil,"OVERLAY");
text:SetPoint("CENTER",5,0);
text:SetFont(STANDARD_TEXT_FONT,9,"OUTLINE");-- Honestly, this is a copy of GameFontWhiteTiny
text:SetTextColor(1,1,1);-- White color
-- Now we store the FontString in our lookup table
TextObjects[statusbar]=text;
end
end
end
end
-- We're avoiding wasting resources on an extra frame by hooking onto the WorldFrame
WorldFrame:HookScript("OnUpdate",function(self)
ProcessWorldFrameChildren(WorldFrame:GetChildren());-- Look for new nameplates and register them
for statusbar,text in pairs(TextObjects) do
-- Check if the nameplate is being rendered, the StatusBar will be visible if so, this is different than :IsShown()
if statusbar:IsVisible() then
local current=statusbar:GetValue();
local _,max=statusbar:GetMinMaxValues();
local percent=math.ceil(100*current/max);
text:SetShown(percent<100);-- This controls when the text is shown
text:SetFormattedText("%d%%",percent);
end
end
end);