Thread Tools Display Modes
01-31-16, 06:01 PM   #1
13ulbasaur
A Deviate Faerie Dragon
 
13ulbasaur's Avatar
Join Date: May 2013
Posts: 14
Changing colour of health text when HP is low.

Hello! I'm making a new interface for myself, and one of the elements I had planned in it is that the health's text, you know the bit that displays the health's percentage, total, current etc., will change to a bright red colour when health is, say, 30% or less. The issue is that, I don't actually know how to do this. I'm planning on using WeakAuras and stuf to make this due to their customizability, so is there a way to make this when HP is <= 30%, the font turns red, otherwise it stays white, with either of those addons, or a separate one? I don't want it to fade into red, which is what happens when i set the colour on stuf to hpthreshold, I just want it to 'pop' become red.

On the topic of fading, does anyone know if its also possible to make it so that the health bar colour also just 'pops' turns into red, rather than fade into red?

I haven't really started making the UI yet (everything I've got so far are all concepts, planning, and an alt with a very messy test UI), so I'm happy to switch to a different unit frame addon if another does better than stuf.

Thank you very much.

Last edited by 13ulbasaur : 01-31-16 at 09:05 PM.
  Reply With Quote
02-01-16, 03:25 AM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
This is just raw Lua code you can run to accomplish this.
Lua Code:
  1. hooksecurefunc("UnitFrameHealthBar_Update",function(bar,unit)
  2.     if bar and not bar.lockValues then
  3.         local cur,_,max=bar:GetValue(),bar:GetMinMaxValues();
  4.         local r,g,b=1,0,0;--    Default red for both bar and text
  5.  
  6.         if cur/max>0.3 then r,g,b=0,1,0; end--  Green bar if >30%
  7.         if not bar.disconnected and not bar.lockColor then bar:SetStatusBarColor(r,g,b); end
  8.  
  9.         if cur/max>0.3 or bar.isZero then r,g,b=1,1,1; end--    White text if dead or >30%
  10.         if bar.TextString then bar.TextString:SetTextColor(r,g,b); end
  11.         if bar.LeftText then bar.LeftText:SetTextColor(r,g,b); end
  12.         if bar.RightText then bar.RightText:SetTextColor(r,g,b); end
  13.     end
  14. end);



Originally Posted by 13ulbasaur View Post
(everything I've got so far are all concepts, planning, and an alt with a very messy test UI)
That's pretty much how mine looks all the time. I still use the default UI with Frankenstein bits and pieces of code I added.
__________________
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 : 02-02-16 at 11:39 PM.
  Reply With Quote
02-01-16, 04:14 AM   #3
13ulbasaur
A Deviate Faerie Dragon
 
13ulbasaur's Avatar
Join Date: May 2013
Posts: 14
Originally Posted by SDPhantom View Post
This is just raw Lua code you can run to accomplish this.
Lua Code:
  1. hooksecurefunc("UnitFrameHealthBar_Update",function(bar,unit)
  2.     if bar and not bar.lockValues then
  3.         local cur,_,max=bar:GetValue,bar:GetMinMaxValues();
  4.         local r,g,b=1,0,0;--    Default red for both bar and text
  5.  
  6.         if cur/max>0.3 then r,g,b=0,1,0; end--  Green bar if >30%
  7.         if not bar.disconnected and not bar.lockColor then bar:SetStatusBarColor(r,g,b); end
  8.  
  9.         if cur/max>0.3 or bar.isZero then r,g,b=1,1,1; end--    White text if dead or >30%
  10.         if bar.TextString then bar.TextString:SetTextColor(r,g,b); end
  11.         if bar.LeftText then bar.LeftText:SetTextColor(r,g,b); end
  12.         if bar.RightText then bar.RightText:SetTextColor(r,g,b); end
  13.     end
  14. end);




That's pretty much how mine looks all the time. I still use the default UI with Frankenstein bits and pieces of code I added.
Hey thanks for this! I appreciate it. However, in all of my noobity, I'm not completely sure how to use it. I tried sticking the code into a custom LUA box for text in stuf but nothing comes up, and I just am not sure where to put it for the bar. Do you think you could give me a little step-by-step?

Sorry! Thanks a lot. And yee frankenstein o/
  Reply With Quote
02-01-16, 02:26 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
It's just pure Lua code, meaning it doesn't run through any other addon. It's addon code by itself. You can either create a folder and ToC file for it or copy/paste the code into http://addon.bool.no to have it done for you.
__________________
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
02-02-16, 11:01 PM   #5
13ulbasaur
A Deviate Faerie Dragon
 
13ulbasaur's Avatar
Join Date: May 2013
Posts: 14
Originally Posted by SDPhantom View Post
It's just pure Lua code, meaning it doesn't run through any other addon. It's addon code by itself. You can either create a folder and ToC file for it or copy/paste the code into http://addon.bool.no to have it done for you.
I see! Thanks a lot. I tried using it with the help of that link, but when I start up the game all I get is this error:

Code:
Date: 2016-02-03 18:01:12
ID: 1
Error occured in: Global
Count: 1
Message: ..\AddOns\MyFirstAddOn\code.lua line 5:
   function arguments expected near ','
Debug:
   
Locals:

AddOns:
  Swatter, v5.0.0 (<%codename%>)
  WowheadLooter, v60009
  Auctionator, v3.2.6
  Babylonian, v5.1.DEV.332(/embedded)
  ColorPickerPlus, v
  Configator, v5.1.DEV.376(/embedded)
  DBMCore, v
  DBMDefaultSkin, v
  DBMStatusBarTimers, v
  eAlign, v
  Engraved, v1.6.1
  EnhancedFriendsList, v1.2
  Gatherer, v5.0.0
  Grid2, vr786
  Grid2AoeHeals, vr786
  Grid2LDB, vr786
  Grid2RaidDebuffs, vr786
  GTFO, v4.37.6
  kgPanels, v1.20
  KuiNameplates, v260
  KuiNameplatesAuras, v
  MasterPlanA, vA
  MikScrollingBattleText, v5.7.143
  MogIt, v3.5.7
  MogItAccessories, v
  mountainomountsmop, vv2.2
  MyFirstAddOn, v
  BlizRuntimeLib_enUS v6.2.3.60200 <none>
  (ck=28c)
and no red when low. Am I supposed to be changing something? I just put in exactly as your code was.

Last edited by 13ulbasaur : 02-02-16 at 11:07 PM.
  Reply With Quote
02-02-16, 11:43 PM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
I made a mistake and left out something. Open the .lua file with a text editor like Notepad and modify the following line:
Code:
local cur,_,max=bar:GetValue(),bar:GetMinMaxValues();
Add the parenthesis colored green.

This fix has been applied to the code posted above as well.
__________________
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 : 02-02-16 at 11:50 PM.
  Reply With Quote
02-03-16, 06:19 AM   #7
13ulbasaur
A Deviate Faerie Dragon
 
13ulbasaur's Avatar
Join Date: May 2013
Posts: 14
Originally Posted by SDPhantom View Post
I made a mistake and left out something. Open the .lua file with a text editor like Notepad and modify the following line:
Code:
local cur,_,max=bar:GetValue(),bar:GetMinMaxValues();
Add the parenthesis colored green.

This fix has been applied to the code posted above as well.
Thanks, no error pops up anymore. But text still remains a lovely white. Is it because I am using stuf? Do I need to change a setting in stuf or something?

Thanks again, and sorry for the hassle.

EDIT: I did manage to find a way to make it pop red. I kinda cheese it by just mimicking the health bar and text with WA and overlaying it on the original, make it red, and make it pop up when health is <=30%, and bam. The only issue is that WA refuses to let the text cover over perfectly so there's awkward little white bits.

Last edited by 13ulbasaur : 02-03-16 at 07:13 AM.
  Reply With Quote
02-03-16, 04:46 PM   #8
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
While conflicts with other addons can happen, it appears not all UnitFrames are running the event-based hook that I used. I changed the hook to come in from a different approach that should run more reliably.
Lua Code:
  1. hooksecurefunc("TextStatusBar_UpdateTextStringWithValues",function(bar,text,val,min,max)
  2.     local name=bar:GetName();
  3.     if name and name:find("HealthBar$") then
  4.         local pcnt=val/max;
  5.         local r,g,b=1,0,0;--    Default red for both bar and text
  6.  
  7.         if pcnt>0.3 then r,g,b=0,1,0; end--  Green bar if >30%
  8.         if not bar.disconnected and not bar.lockColor then bar:SetStatusBarColor(r,g,b); end
  9.  
  10.         if pcnt>0.3 or pcnt<=0 then r,g,b=1,1,1; end--    White text if dead or >30%
  11.         if bar.LeftText then bar.LeftText:SetTextColor(r,g,b); end
  12.         if bar.RightText then bar.RightText:SetTextColor(r,g,b); end
  13.         text:SetTextColor(r,g,b);
  14.     end
  15. 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

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Changing colour of health text when HP is low.


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