Thread Tools Display Modes
05-24-18, 06:23 PM   #1
Xancepants
A Deviate Faerie Dragon
Join Date: Oct 2014
Posts: 17
Personal Resource Bar: HP Color by % missing?

Hello all, I'm aware of a few addons that can achieve what I am looking for, such as KuiNamePlates, but I was wondering if any programming language guru(s) out there might know how to make a Macro / Script, or some LUA Code to make the Health on your Personal Resource Bar change color, based on % Value of missing HP? (That's the HP/MANA bar that appears directly below your character). For example:

100% = Green
75% = Green/Yellow
50% = Yellow
25% = Orange
>25% = Red



I found a similar script that does the same thing, but it's for Player Frame and Target Frame. Here is the code if it helps to give anybody an idea or starting point.


--[Color HP Bars Green at Full & Red at Low Health]
hooksecurefunc("HealthBar_OnValueChanged",function(self,value)
local min,max = self:GetMinMaxValues()
if not value or value<min or value>max then
return
end
-- change value to a percentage
value = (max-min)>0 and (value-min)/(max-min) or 0
-- recolor based on percentage
if value>0.5 then
self:SetStatusBarColor((1-value)*2,1,0)
else
self:SetStatusBarColor(1,value*2,0)
end
end)



Not sure if this is the correct place for this question and I apologize if it's not, but it seemed like the closest match! Any Help is greatly appreciated, and thank you in advance
  Reply With Quote
05-24-18, 07:19 PM   #2
Ammako
A Frostmaul Preserver
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 256
That should do it, up to you to check more thoroughly to see if using SetStatusBarColor on nameplates doesn't taint anything in a bad way though (and to check whether it doesn't leave random other nameplates colored wrong, but it shouldn't.)

lua Code:
  1. hooksecurefunc("CompactUnitFrame_UpdateHealth", function(frame)
  2.     if frame.optionTable.colorNameBySelection and not frame:IsForbidden() then
  3.         local healthPercentage = ceil((UnitHealth(frame.displayedUnit) / UnitHealthMax(frame.displayedUnit) * 100))
  4.        
  5.         if C_NamePlate.GetNamePlateForUnit(frame.unit) == C_NamePlate.GetNamePlateForUnit("player") then
  6.             if healthPercentage == 100 then
  7.                 frame.healthBar:SetStatusBarColor(0, 1, 0)
  8.             elseif healthPercentage < 100 and healthPercentage >= 75 then
  9.                 frame.healthBar:SetStatusBarColor(0.6, 0.8, 0.19)
  10.             elseif healthPercentage < 75 and healthPercentage >= 50 then
  11.                 frame.healthBar:SetStatusBarColor(1, 1, 0)
  12.             elseif healthPercentage < 50 and healthPercentage >= 25 then
  13.                 frame.healthBar:SetStatusBarColor(1, 0.64, 0)
  14.             elseif healthPercentage < 25 then
  15.                 frame.healthBar:SetStatusBarColor(1, 0, 0)
  16.             end
  17.         end
  18.     end
  19. end)

Hacky, but eh. Not like that other script was any better.

Last edited by Ammako : 05-24-18 at 07:22 PM.
  Reply With Quote
05-24-18, 08:16 PM   #3
Xancepants
A Deviate Faerie Dragon
Join Date: Oct 2014
Posts: 17

<3 <3 <3 <3 <3 <3 <3 <3

Works Flawlessly! Thank you very much for the help and quick reply!

Ya know... people should start offering to pay for having things custom scripted/coded for them in the form of WoW Game Time Tokens. Is that possible? If so... does that violate forum rules in some way?
  Reply With Quote
05-24-18, 08:46 PM   #4
Ammako
A Frostmaul Preserver
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 256
Tipping doesn't break this forum's rules on its own, but WoW token would imply in-game gold, and that would be breaking Blizzard rules if you were giving that in exchange for out-of-game services (which addon development falls under.)
Gifting directly from the battle.net shop (or gifting battle.net balance directly) would be the way to go for what you are suggesting.

Still, I don't think this should be the norm whenever someone asks for help on this forum. There's nothing wrong with it but there's nothing wrong with just nicely asking for help either.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Personal Resource Bar: HP Color by % missing?

Thread Tools
Display Modes

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