View Single Post
03-04-14, 09:23 PM   #11
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
UnitReaction can return nil. Here's a version based on the coloring in my oUF layout that will do class colors for players, or gray for tapped units, or reaction colors for other units, with a fallback in case of invalid class/reaction data:

Lua Code:
  1. hooksecurefunc("UnitFrame_Update", function(self)
  2.     if not self.name then return end
  3.  
  4.     local color
  5.     if UnitIsPlayer(unit) then
  6.         local _, class = UnitClass(unit)
  7.         color = (CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)[class]
  8.     elseif UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit) then
  9.         color = GRAY_FONT_COLOR
  10.     elseif UnitIsEnemy(unit, "player") then
  11.         color = FACTION_BAR_COLORS[1]
  12.     else
  13.         local reaction = UnitReaction(unit, "player")
  14.         color = reaction and FACTION_BAR_COLORS[reaction] or FACTION_BAR_COLORS[5]
  15.     end
  16.  
  17.     if not color then
  18.         color = NORMAL_FONT_COLOR
  19.     end
  20.  
  21.     self.name:SetTextColor(color.r, color.g, color.b)
  22. end)
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote