WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   Personal Resource Bar: HP Color by % missing? (https://www.wowinterface.com/forums/showthread.php?t=56237)

Xancepants 05-24-18 06:23 PM

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 :)

Ammako 05-24-18 07:19 PM

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. :p

Xancepants 05-24-18 08:16 PM

:banana::banana::banana:
<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?

Ammako 05-24-18 08:46 PM

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.


All times are GMT -6. The time now is 06:30 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI