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
04-13-14, 11:06 PM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Actually upon further investigation I'm not really sure what your goal with this code is. Nothing in the Blizzard UI ever changes these textures after creation, so after (for example) you target a player unit once, the texture on the target frame is permanently changed -- it never changes back for NPCs.

If your goal is to permanently change the texture, then the problem you're having is caused by the fact that you are not changing the texture until the frame is displaying a player unit. To fix this, get rid of all the event handling and unit checking, and just set the textures once when your addon loads:

Code:
for _, region in pairs({
	TargetFrameNameBackground,
	FocusFrameNameBackground,
	Boss1TargetFrameNameBackground,
	Boss2TargetFrameNameBackground,
	Boss3TargetFrameNameBackground,
	Boss4TargetFrameNameBackground,
}) do
	region:SetTexture("Interface\\DialogFrame\\UI-DialogBox-Background")
end
If your goal is to change the texture only for players, then you need to re-set it when the unit isn't a player:

Code:
local unitRegions = {
	target = TargetFrameNameBackground,
	focus = FocusFrameNameBackground,
	boss1 = Boss1TargetFrameNameBackground,
	boss2 = Boss2TargetFrameNameBackground,
	boss3 = Boss3TargetFrameNameBackground,
	boss4 = Boss4TargetFrameNameBackground,
}

local f = CreateFrame("Frame")
f:RegisterEvent("GROUP_ROSTER_UPDATE")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:RegisterEvent("PLAYER_FOCUS_CHANGED")
f:RegisterEvent("PLAYER_TARGET_CHANGED")
f:RegisterEvent("UNIT_FACTION")
f:SetScript("OnEvent", function(self, event)
	for unit, region in pairs(unitRegions) do
		if UnitIsPlayer(unit) then
			region:SetTexture("Interface\\DialogFrame\\UI-DialogBox-Background")
		else
			region:SetTexture("Interface\\TargetingFrame\\UI-TargetingFrame-LevelBackground")
		end
	end
end)
I tested both of these and they seem to be working as expected.
__________________
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
04-14-14, 09:27 AM   #10
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
Actually upon further investigation I'm not really sure what your goal with this code is. Nothing in the Blizzard UI ever changes these textures after creation, so after (for example) you target a player unit once, the texture on the target frame is permanently changed -- it never changes back for NPCs.

If your goal is to permanently change the texture, then the problem you're having is caused by the fact that you are not changing the texture until the frame is displaying a player unit. To fix this, get rid of all the event handling and unit checking, and just set the textures once when your addon loads:

Code:
for _, region in pairs({
	TargetFrameNameBackground,
	FocusFrameNameBackground,
	Boss1TargetFrameNameBackground,
	Boss2TargetFrameNameBackground,
	Boss3TargetFrameNameBackground,
	Boss4TargetFrameNameBackground,
}) do
	region:SetTexture("Interface\\DialogFrame\\UI-DialogBox-Background")
end
If your goal is to change the texture only for players, then you need to re-set it when the unit isn't a player:

Code:
local unitRegions = {
	target = TargetFrameNameBackground,
	focus = FocusFrameNameBackground,
	boss1 = Boss1TargetFrameNameBackground,
	boss2 = Boss2TargetFrameNameBackground,
	boss3 = Boss3TargetFrameNameBackground,
	boss4 = Boss4TargetFrameNameBackground,
}

local f = CreateFrame("Frame")
f:RegisterEvent("GROUP_ROSTER_UPDATE")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:RegisterEvent("PLAYER_FOCUS_CHANGED")
f:RegisterEvent("PLAYER_TARGET_CHANGED")
f:RegisterEvent("UNIT_FACTION")
f:SetScript("OnEvent", function(self, event)
	for unit, region in pairs(unitRegions) do
		if UnitIsPlayer(unit) then
			region:SetTexture("Interface\\DialogFrame\\UI-DialogBox-Background")
		else
			region:SetTexture("Interface\\TargetingFrame\\UI-TargetingFrame-LevelBackground")
		end
	end
end)
I tested both of these and they seem to be working as expected.
I wanted to just change the texture all together so the first code is what I'm going to use.

Damn how many times can a single person tell another thanks in one day

Coke
  Reply With Quote

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

Thread Tools
Display Modes

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