Thread Tools Display Modes
08-07-09, 02:15 AM   #1
Wimpface
A Molten Giant
 
Wimpface's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 648
Help with creating a health tag.

So, I've been unable to find any help regarding this for the last few days and I've not found it in any layout so far to take references from.

What I want is a tag that changes color (from green to red) based on the units health, I've tried a bit with oUF.ColorGradient but it has not worked out well for me. (i.e I've done something wrong.)

If anyone would be just as so kind to help me out with this, I'd appreciate it a lot.

Regards,
Rhiren.
__________________
All I see is strobe lights blinding me in my hindsight.
  Reply With Quote
08-07-09, 02:24 AM   #2
Freebaser
A Molten Kobold Bandit
 
Freebaser's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2008
Posts: 135
Originally Posted by Wimpface View Post
So, I've been unable to find any help regarding this for the last few days and I've not found it in any layout so far to take references from.

What I want is a tag that changes color (from green to red) based on the units health, I've tried a bit with oUF.ColorGradient but it has not worked out well for me. (i.e I've done something wrong.)

If anyone would be just as so kind to help me out with this, I'd appreciate it a lot.

Regards,
Rhiren.
oUF_Freeb uses ColorGradient and you can glance at it here if it helps.
  Reply With Quote
08-07-09, 02:27 AM   #3
Wimpface
A Molten Giant
 
Wimpface's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 648
Thanks, can't believe I overlooked it.

Cheers.
__________________
All I see is strobe lights blinding me in my hindsight.
  Reply With Quote
08-07-09, 02:33 AM   #4
Freebaser
A Molten Kobold Bandit
 
Freebaser's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2008
Posts: 135
Originally Posted by Wimpface View Post
Thanks, can't believe I overlooked it.

Cheers.
Also, don't get confused in my updateHealth function. There are 2 different ColorGradients, r,g,b applies to text and x,y,z to the health bar.
  Reply With Quote
08-07-09, 02:37 AM   #5
Wimpface
A Molten Giant
 
Wimpface's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 648
EDIT: I added "self" into the function, and now I get a different error. I'll paste the code/error below here.

Code:
oUF.Tags['[rhealth]'] = function(self, unit)
	local min, max = UnitHealth(unit), UnitHealthMax(unit)
	
	local status = not UnitIsConnected(unit) and 'Offline' or UnitIsGhost(unit) and 'Ghost' or UnitIsDead(unit) and 'Dead'
	if(max ~= 0) then
		r, g, b = self.ColorGradient(min/max, .69,.31,.31, .65,.63,.35, .33,.59,.33)
	end
	
	return status and status or min ~= max and ('%s'):format(shortVal(min), shortVal(max)) or max
end

oUF.TagEvents['[rhealth]'] = 'UNIT_HEALTH'
Code:
Message: Interface\AddOns\oUF_Rhiren\tags.lua:38: Usage: UnitHealth("unit")
Time: 08/07/09 10:53:01
Count: 1
Stack: [string "Interface\FrameXML\BasicControls.xml:<Scrip..."]:18: in function <[string "Interface\FrameXML\BasicControls.xml:<Scrip..."]:4>
[C]: ?
[C]: in function `UnitHealth'
Interface\AddOns\oUF_Rhiren\tags.lua:38: in function `func'
Interface\AddOns\oUF\elements\tags.lua:305: in function `UpdateTag'
Interface\AddOns\oUF\elements\tags.lua:178: in function `func'
Interface\AddOns\oUF\ouf.lua:484: in function <Interface\AddOns\oUF\ouf.lua:479>
(tail call): ?

Locals: (*temporary) = nil
__________________
All I see is strobe lights blinding me in my hindsight.

Last edited by Wimpface : 08-07-09 at 02:55 AM.
  Reply With Quote
08-07-09, 02:53 AM   #6
Freebaser
A Molten Kobold Bandit
 
Freebaser's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2008
Posts: 135
change self to oUF.ColorGradient

Edit: your return status needs some work, but I don't know a fix off the top of my head. *sleepy*

Last edited by Freebaser : 08-07-09 at 02:57 AM.
  Reply With Quote
08-07-09, 02:56 AM   #7
Wimpface
A Molten Giant
 
Wimpface's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 648
Originally Posted by Freebaser View Post
change self to oUF.ColorGradient
Cheers, however, it does not seem to do any coloring at all now but I don't get any errors. Could it be that I'm using a "short value" function?

Code:
oUF.Tags['[rhealth]'] = function(unit)
	local min, max = UnitHealth(unit), UnitHealthMax(unit)
	
	local status = not UnitIsConnected(unit) and 'Offline' or UnitIsGhost(unit) and 'Ghost' or UnitIsDead(unit) and 'Dead'
	if(max ~= 0) then
		r, g, b = oUF.ColorGradient(min/max, .69,.31,.31, .65,.63,.35, .33,.59,.33)
	end
	
	return status and status or min ~= max and ('%s'):format(shortVal(min), shortVal(max)) or max
end
__________________
All I see is strobe lights blinding me in my hindsight.
  Reply With Quote
08-07-09, 04:02 PM   #8
Subere
A Cyclonian
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 41
Originally Posted by Wimpface View Post
Cheers, however, it does not seem to do any coloring at all now but I don't get any errors. Could it be that I'm using a "short value" function?

Code:
oUF.Tags['[rhealth]'] = function(unit)
	local min, max = UnitHealth(unit), UnitHealthMax(unit)
	
	local status = not UnitIsConnected(unit) and 'Offline' or UnitIsGhost(unit) and 'Ghost' or UnitIsDead(unit) and 'Dead'
	if(max ~= 0) then
		r, g, b = oUF.ColorGradient(min/max, .69,.31,.31, .65,.63,.35, .33,.59,.33)
	end
	
	return status and status or min ~= max and ('%s'):format(shortVal(min), shortVal(max)) or max
end
Just guessing it's because you're not actually doing anything with the color you're grabbing.

Something like below should work.

Code:
-- If you already have a function like this in your layout you don't need to add it.
local function Hex(r, g, b)
	if type(r) == 'table' then
		if r.r then r, g, b = r.r, r.g, r.b else r, g, b = unpack(r) end
	end
	
	return string.format('|cff%02x%02x%02x', r*255, g*255, b*255)
end

oUF.Tags['[rhealth]'] = function(unit)
	local min, max = UnitHealth(unit), UnitHealthMax(unit)
	local r, g, b
	
	local status = not UnitIsConnected(unit) and 'Offline' or UnitIsGhost(unit) and 'Ghost' or UnitIsDead(unit) and 'Dead'
	
	if(max ~= 0) then
		r, g, b = oUF.ColorGradient(min/max, .69,.31,.31, .65,.63,.35, .33,.59,.33)
	end
	
	return status and status or min ~= max and ('%s%s / %s|r'):format(Hex(r, g, b), shortVal(min), shortVal(max)) or ('%s%d|r'):format(Hex(r, g, b), max)
end
  Reply With Quote
08-07-09, 06:16 PM   #9
Waverian
A Chromatic Dragonspawn
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 188
If you want green -> red you can just use oUF's predefined color table, smooth more specifically.

Code:
	if max ~= 0 then
		r, g, b = oUF.ColorGradient(min / max, unpack(oUF.colors.smooth))
	end
	
	return format("|cff%02x%02x%02x%s%s|r", r * 255, g * 255, b * 255, floor((min / max) * 100), "%")
That's hp percent obviously but the concept still applies.
  Reply With Quote
08-08-09, 02:38 AM   #10
Wimpface
A Molten Giant
 
Wimpface's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 648
Cheers people, thanks for all the help.
__________________
All I see is strobe lights blinding me in my hindsight.
  Reply With Quote
08-08-09, 02:53 AM   #11
Wimpface
A Molten Giant
 
Wimpface's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 648
As said, cheers for all the help. It's deeply appreciated, however, now I get another error.
Either I'm a complete tard that should just stay away from coding or something just went wrong somewhere.

Code:
Message: Interface\AddOns\oUF_Rhiren\tags.lua:47: attempt to call global 'Hex' (a nil value)
Time: 08/08/09 10:50:12
Count: 1
Stack: [string "Interface\FrameXML\BasicControls.xml:<Scrip..."]:18: in function <[string "Interface\FrameXML\BasicControls.xml:<Scrip..."]:4>
[C]: in function `Hex'
Interface\AddOns\oUF_Rhiren\tags.lua:47: in function `func'
Interface\AddOns\oUF\elements\tags.lua:305: in function `UpdateTag'
Interface\AddOns\oUF\elements\tags.lua:178: in function `func'
Interface\AddOns\oUF\ouf.lua:484: in function <Interface\AddOns\oUF\ouf.lua:479>
(tail call): ?

Locals: unit = "player"
min = 15800
max = 15800
r = 0.33
g = 0.59
b = 0.33
status = nil
(*temporary) = <function> defined =[C]:-1
(*temporary) = "%s%d"
(*temporary) = nil
(*temporary) = 0.33
(*temporary) = 0.59
(*temporary) = 0.33
(*temporary) = "attempt to call global 'Hex' (a nil value)"
ColorGradient = <function> defined @Interface\AddOns\oUF\ouf.lua:489
shortVal = <function> defined @Interface\AddOns\oUF_Rhiren\tags.lua:20
Here's the pastey to my entire "tags.lua" file.
http://pastey.net/120369

Again, thanks for all the help.
__________________
All I see is strobe lights blinding me in my hindsight.
  Reply With Quote
08-08-09, 04:57 AM   #12
Rostok
A Flamescale Wyrmkin
Join Date: Jul 2008
Posts: 127
Hi,
it seems that the function is case sensitive

Hex <> hex.

Regards.
  Reply With Quote
08-08-09, 02:43 PM   #13
Wimpface
A Molten Giant
 
Wimpface's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 648
Originally Posted by Rostok View Post
Hi,
it seems that the function is case sensitive

Hex <> hex.

Regards.
Cheers, /slapself for not seeing it.
__________________
All I see is strobe lights blinding me in my hindsight.
  Reply With Quote

WoWInterface » Featured Projects » oUF (Otravi Unit Frames) » Help with creating a health tag.


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