Thread Tools Display Modes
03-13-14, 05:20 PM   #1
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 325
Unitframe Name Background

Is there better code way to do this besides the following?

Lua Code:
  1. local frame = CreateFrame("FRAME")
  2. frame:RegisterEvent("GROUP_ROSTER_UPDATE")
  3. frame:RegisterEvent("PLAYER_TARGET_CHANGED")
  4. frame:RegisterEvent("PLAYER_FOCUS_CHANGED")
  5. frame:RegisterEvent("UNIT_FACTION")
  6.  
  7. local function eventHandler(self, event, ...)
  8.     if UnitIsPlayer("target") then
  9.         c = { 0, 0, 0 }
  10.         TargetFrameNameBackground:SetTexture([[Interface\DialogFrame\UI-DialogBox-Background]])
  11.     end
  12.     if UnitIsPlayer("focus") then
  13.         c = { 0, 0, 0 }
  14.         FocusFrameNameBackground:SetTexture([[Interface\DialogFrame\UI-DialogBox-Background]])
  15.     end
  16. end
  17.  
  18. frame:SetScript("OnEvent", eventHandler)

Thanks in advance.
Coke
  Reply With Quote
03-13-14, 07:28 PM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
A few questions.
  • Is the variable 'c' a local variable, and not in the global namespace?
  • 'c' is not descriptive. What does it do? Where do you use it? In a year, 'c' will not mean anything to you, nor anyone else. If it is colours, call it 'MyAddOnName_UnitColors' or something that tells you what use it serves.
  • In your event handler function, you are not checking for if event == "EVENT_NAME" then...
  • Since you are only checking for focus and target, why register the other events?

As for "is there a better way", if you could be more forthcoming about what you are trying to do overall, that would help. Right now, you haven't posted enough code to properly answer.

Last edited by myrroddin : 03-13-14 at 07:32 PM. Reason: added something
  Reply With Quote
03-13-14, 10:36 PM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
1. Your "c" variable is currently global. Globals are bad, mmkay.

2. You're currently creating a new table every time your function runs. This is also bad. Create the table once, and reference it inside your function.

3. You're not even doing anything with that table, so just get rid of it.

4. There's nothing wrong with your event handler (I think Myrroddin misread what you're doing there, as you're not checking any event arguments, you're checking whether the specified unit tokens currently belong to player units) but you don't need to define it outside of the SetScript call unless you're going to be doing something else with it (such as using the same function on another frame). The way you're doing it won't hurt anything, it's just unnecessarily messy.

Code:
frame:SetScript("OnEvent", function(self, event, ...)
     -- Do your thing here.
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
03-14-14, 06:36 AM   #4
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Don't know about "better" way, but slightly prettier, perhaps.
Code:
local frame = CreateFrame("FRAME")
frame:RegisterEvent("GROUP_ROSTER_UPDATE")
frame:RegisterEvent("PLAYER_TARGET_CHANGED")
frame:RegisterEvent("PLAYER_FOCUS_CHANGED")
frame:RegisterEvent("UNIT_FACTION")

local unitRegions = {
	target = TargetFrameNameBackground,
	focus = FocusFrameNameBackground,
}

frame:SetScript("OnEvent", function(self, event, ...)
	for unit, region in pairs(unitRegions) do
		if UnitIsPlayer(unit) then
			c = { 0, 0, 0 } -- ??
			region:SetTexture([[Interface\DialogFrame\UI-DialogBox-Background]])
		end
	end
end)
__________________
Grab your sword and fight the Horde!
  Reply With Quote
03-15-14, 07:25 AM   #5
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 325
Originally Posted by Lombra View Post
Don't know about "better" way, but slightly prettier, perhaps.
Code:
local frame = CreateFrame("FRAME")
frame:RegisterEvent("GROUP_ROSTER_UPDATE")
frame:RegisterEvent("PLAYER_TARGET_CHANGED")
frame:RegisterEvent("PLAYER_FOCUS_CHANGED")
frame:RegisterEvent("UNIT_FACTION")

local unitRegions = {
	target = TargetFrameNameBackground,
	focus = FocusFrameNameBackground,
}

frame:SetScript("OnEvent", function(self, event, ...)
	for unit, region in pairs(unitRegions) do
		if UnitIsPlayer(unit) then
			c = { 0, 0, 0 } -- ??
			region:SetTexture([[Interface\DialogFrame\UI-DialogBox-Background]])
		end
	end
end)
Thank You for this it works perfectly.

Coke
  Reply With Quote
03-23-14, 05:02 PM   #6
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 325
Not sure why but the background does not change until I click on my own portrait.

Here is the current code:
Lua Code:
  1. local frame = CreateFrame("FRAME")
  2.         frame:RegisterEvent("GROUP_ROSTER_UPDATE")
  3.         frame:RegisterEvent("PLAYER_TARGET_CHANGED")
  4.         frame:RegisterEvent("PLAYER_FOCUS_CHANGED")
  5.         frame:RegisterEvent("UNIT_FACTION")
  6.  
  7.         local unitRegions = {
  8.             target = TargetFrameNameBackground,
  9.             focus = FocusFrameNameBackground,
  10.             boss1 = Boss1TargetFrameNameBackground,
  11.             boss2 = Boss2TargetFrameNameBackground,
  12.             boss3 = Boss3TargetFrameNameBackground,
  13.             boss4 = Boss4TargetFrameNameBackground,
  14.         }
  15.  
  16.         frame:SetScript("OnEvent", function(self, event, ...)
  17.             for unit, region in pairs(unitRegions) do
  18.                 if UnitIsPlayer(unit) then
  19.                     region:SetTexture('Interface\\DialogFrame\\UI-DialogBox-Background')
  20.                 end
  21.             end
  22.         end)
  Reply With Quote
03-23-14, 06:12 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Try adding PLAYER_ENTERING_WORLD to the list of registered events.
__________________
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
03-23-14, 10:30 PM   #8
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 325
Originally Posted by Phanx View Post
Try adding PLAYER_ENTERING_WORLD to the list of registered events.
Still don't work until I click my own unitframe then it works.

Not a huge problem just a pesky one.

Coke
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Unitframe Name Background


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