Thread Tools Display Modes
08-25-15, 10:55 PM   #1
Lesteryoung
A Black Drake
Join Date: Aug 2015
Posts: 81
Adding something to a small addon.

Hey, I found and partially made this addon that adds percent health to the default nameplates. I want to add to it that the percent only show on nameplates that are less than 100%. Could someone tell me how to do this or just do it for me? Much appreciated if anyone responds.

Here's the code:

Code:
CreateFrame('frame'):SetScript('OnUpdate', function(self, elapsed)
  for index = 1, select('#', WorldFrame:GetChildren()) do
        local f = select(index, WorldFrame:GetChildren())
        if f:GetName() and f:GetName():find('NamePlate%d') then
          f.h = select(1, select(1, f:GetChildren()):GetChildren())
          if f.h then
         if not f.h.v then
           f.h.v = f.h:CreateFontString(nil, "ARTWORK")  
           f.h.v:SetPoint("CENTER", 5, 0)
           f.h.v:SetFont(STANDARD_TEXT_FONT, 9, 'OUTLINE')
         else
           local _, maxh = f.h:GetMinMaxValues()
           local val = f.h:GetValue()
           f.h.v:SetText(string.format(math.floor((val/maxh)*100)).." %")
         end
          end
        end
  end
end)
  Reply With Quote
08-26-15, 12:40 AM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
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:
  1. --  This table will link the StatusBars to our FontStrings
  2. --  This is to keep the nameplate tables clean
  3. local TextObjects={};
  4.  
  5. --  This processes the WorldFrame's children and populates our lookup table
  6. local function ProcessWorldFrameChildren(...)
  7. --  Scan through the children listed in our args
  8. --  Dynamic indexing of arguments is the only practical use of select()
  9.     for i=1,select("#",...) do
  10.         local nameplate=select(i,...);
  11.  
  12. --      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
  13.         local name=nameplate:GetName();
  14.         if name and name:find("^NamePlate%d+$") then
  15. --          No use of select() here, only the first return of these function calls are used anyway
  16.             local statusbar=nameplate:GetChildren():GetChildren();
  17.  
  18. --          If we don't have a FontString for this StatusBar, we create one
  19.             if not TextObjects[statusbar] then
  20.                 local text=statusbar:CreateFontString(nil,"OVERLAY");
  21.                 text:SetPoint("CENTER",5,0);
  22.                 text:SetFont(STANDARD_TEXT_FONT,9,"OUTLINE");-- Honestly, this is a copy of GameFontWhiteTiny
  23.                 text:SetTextColor(1,1,1);-- White color
  24.  
  25. --              Now we store the FontString in our lookup table
  26.                 TextObjects[statusbar]=text;
  27.             end
  28.         end
  29.     end
  30. end
  31.  
  32. --  We're avoiding wasting resources on an extra frame by hooking onto the WorldFrame
  33. WorldFrame:HookScript("OnUpdate",function(self)
  34.     ProcessWorldFrameChildren(WorldFrame:GetChildren());--  Look for new nameplates and register them
  35.     for statusbar,text in pairs(TextObjects) do
  36. --      Check if the nameplate is being rendered, the StatusBar will be visible if so, this is different than :IsShown()
  37.         if statusbar:IsVisible() then
  38.             local current=statusbar:GetValue();
  39.             local _,max=statusbar:GetMinMaxValues();
  40.             local percent=math.ceil(100*current/max);
  41.  
  42.             text:SetShown(percent<100);--   This controls when the text is shown
  43.             text:SetFormattedText("%d%%",percent);
  44.         end
  45.     end
  46. end);
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
08-26-15, 12:17 PM   #3
Tonyleila
A Molten Giant
 
Tonyleila's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 758
Have you Seen this one?
http://www.wowinterface.com/download...ameplates.html
__________________
Author of: LeilaUI and Aurora: Missing Textures
__________________
  Reply With Quote
08-26-15, 02:00 PM   #4
Lesteryoung
A Black Drake
Join Date: Aug 2015
Posts: 81
Thanks so much Phantom!

Works fine and I learned a few things in the process.

I have a quick question, can I remove the text you wrote and take any blank space out to make it look cleaner. I'll use this addon forever so just asking in case I can make it look better in notepad.

Hopefully not being too picky. If I am don't bother responding. Thanks again!
  Reply With Quote
08-26-15, 02:04 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Originally Posted by Tonyleila View Post
This is the exact code the OP posted, he wanted a modification to only show when below 100%.



Originally Posted by Lesteryoung View Post
I have a quick question, can I remove the text you wrote and take any blank space out to make it look cleaner. I'll use this addon forever so just asking in case I can make it look better in notepad.
It'll work fine without the comments. They were just to point out improvements made with the new code and describe what it does. The blank lines, while unnecessary as well, make the code look better by visually defining blocks of code that perform a specific purpose.

PS: While Notepad does get the job done, you might want to look into a better IDE to use like Notepad++ or SciTE. Both are free and come with useful features for writing code.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 08-26-15 at 02:17 PM.
  Reply With Quote
08-26-15, 05:44 PM   #6
Lesteryoung
A Black Drake
Join Date: Aug 2015
Posts: 81
Hey, can you set the font as a decimal or will it just round up or down to the nearest whole number?
  Reply With Quote
08-26-15, 08:12 PM   #7
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
The height is measured in pixels, so anything other than a whole integer shouldn't visibly do anything.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Adding something to a small addon.


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off