Thread Tools Display Modes
04-13-19, 09:52 AM   #1
SilverDragony
A Murloc Raider
Join Date: Apr 2019
Posts: 6
Create target 2D portrait

Hi,

I try to create un portrait for player and target. I've created un code, te target portrait exists but there isn't image.

Code:
--Player Frame
local fp = CreateFrame("Button", "FarmootPlayerPortrait", UIParent, "SecureUnitButtonTemplate")
fp:SetSize(70, 70)
fp:SetPoint("CENTER", -100, -320)
fp:SetAttribute("unit", "player")
RegisterUnitWatch(fp)
fp:SetAttribute("toggleForVehicle", true)
fp:RegisterForClicks("AnyUp")
fp:SetAttribute("*type1", "target")
fp:SetAttribute("*type2", "togglemenu")
fp:SetAttribute("*type3", "assist")
fp.Texture = fp:CreateTexture("$parent_Texture", "BACKGROUND")
fp.Texture:SetAllPoints()
SetPortraitTexture(fp.Texture, "player")
fp.Border = fp:CreateTexture("$parent_Border", "BORDER")
fp.Border:SetPoint("TOPLEFT", -6, 4)
fp.Border:SetPoint("BOTTOMRIGHT", 6, -10)
fp.Border:SetTexture("Interface/PLAYERFRAME/UI-PlayerFrame-Deathknight-Ring")
fp.Border:SetVertexColor(1, 1, 0, 1)
fp:RegisterUnitEvent("UNIT_PORTRAIT_UPDATE", "player")
fp:SetScript("OnEvent", function(self) SetPortraitTexture(self.Texture, "player") end)

--Target Frame
local ft = CreateFrame("Button", "FarmootPlayerPortrait", UIParent, "SecureUnitButtonTemplate")
ft:SetSize(50, 50)
ft:SetPoint("CENTER", 180, 0)
ft:SetAttribute("unit", "target")
RegisterUnitWatch(ft)
ft:SetAttribute("toggleForVehicle", true)
ft:RegisterForClicks("AnyUp")
fp:SetAttribute("*type1", "target")
ft:SetAttribute("*type2", "togglemenu")
ft:SetAttribute("*type3", "assist")
ft.Texture = ft:CreateTexture("$parent_Texture", "BACKGROUND")
ft.Texture:SetAllPoints()
SetPortraitTexture(ft.Texture, "target")
ft.Border = ft:CreateTexture("$parent_Border", "BORDER")
ft.Border:SetPoint("TOPLEFT", -6, 4)
ft.Border:SetPoint("BOTTOMRIGHT", 6, -10)
ft.Border:SetTexture("Interface/PLAYERFRAME/UI-PlayerFrame-Deathknight-Ring")
ft.Border:SetVertexColor(1, 1, 0, 1)
ft:RegisterUnitEvent("UNIT_PORTRAIT_UPDATE", "target")
ft:SetScript("OnEvent", function(self) SetPortraitTexture(self.Texture, "target") end)
That's the code, do you know what's the problem for display the image of target portrait ?

Thank you for your help.
  Reply With Quote
04-13-19, 10:07 AM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
You need to change the portrait when your target changes (and you had one fp.xxx in your ft.xxx section)

Eg.
Lua Code:
  1. --Player Frame
  2. local fp = CreateFrame("Button", "FarmootPlayerPortrait", UIParent, "SecureUnitButtonTemplate")
  3. fp:SetSize(70, 70)
  4. fp:SetPoint("CENTER", -100, -320)
  5. fp:SetAttribute("unit", "player")
  6. RegisterUnitWatch(fp)
  7. fp:SetAttribute("toggleForVehicle", true)
  8. fp:RegisterForClicks("AnyUp")
  9. fp:SetAttribute("*type1", "target")
  10. fp:SetAttribute("*type2", "togglemenu")
  11. fp:SetAttribute("*type3", "assist")
  12. fp.Texture = fp:CreateTexture("$parent_Texture", "BACKGROUND")
  13. fp.Texture:SetAllPoints()
  14. SetPortraitTexture(fp.Texture, "player")
  15. fp.Border = fp:CreateTexture("$parent_Border", "BORDER")
  16. fp.Border:SetPoint("TOPLEFT", -6, 4)
  17. fp.Border:SetPoint("BOTTOMRIGHT", 6, -10)
  18. fp.Border:SetTexture("Interface/PLAYERFRAME/UI-PlayerFrame-Deathknight-Ring")
  19. fp.Border:SetVertexColor(1, 1, 0, 1)
  20. fp:RegisterUnitEvent("UNIT_PORTRAIT_UPDATE", "player")
  21. fp:RegisterEvent("PLAYER_TARGET_CHANGED") -- Added
  22. fp:SetScript("OnEvent", function(self)
  23.     if event == "UNIT_PORTRAIT_UPDATE" then
  24.         SetPortraitTexture(self.Texture, "player")
  25.     else
  26.         SetPortraitTexture(self.Target.Texture, "target") -- Added
  27.     end
  28. end)
  29.  
  30. --Target Frame
  31. local ft = CreateFrame("Button", "FarmootPlayerPortrait", UIParent, "SecureUnitButtonTemplate")
  32. fp.Target = ft -- Added
  33. ft:SetSize(50, 50)
  34. ft:SetPoint("CENTER", 180, 0)
  35. ft:SetAttribute("unit", "target")
  36. RegisterUnitWatch(ft)
  37. ft:SetAttribute("toggleForVehicle", true)
  38. ft:RegisterForClicks("AnyUp")
  39. ft:SetAttribute("*type1", "target")
  40. ft:SetAttribute("*type2", "togglemenu")
  41. ft:SetAttribute("*type3", "assist")
  42. ft.Texture = ft:CreateTexture("$parent_Texture", "BACKGROUND")
  43. ft.Texture:SetAllPoints()
  44. SetPortraitTexture(ft.Texture, "target")
  45. ft.Border = ft:CreateTexture("$parent_Border", "BORDER")
  46. ft.Border:SetPoint("TOPLEFT", -6, 4)
  47. ft.Border:SetPoint("BOTTOMRIGHT", 6, -10)
  48. ft.Border:SetTexture("Interface/PLAYERFRAME/UI-PlayerFrame-Deathknight-Ring")
  49. ft.Border:SetVertexColor(1, 1, 0, 1)
  50. ft:RegisterUnitEvent("UNIT_PORTRAIT_UPDATE", "target")
  51. ft:SetScript("OnEvent", function(self) SetPortraitTexture(self.Texture, "target")  end)
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 04-13-19 at 10:44 AM.
  Reply With Quote
04-13-19, 04:09 PM   #3
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by Fizzlemizz View Post
You need to change the portrait when your target changes (and you had one fp.xxx in your ft.xxx section)
You could achieve the same result just by having the target frame register for PLAYER_TARGET_CHANGED in addition to UNIT_PORTRAIT_UPDATE. The script should still be able to handle the event as-is. No need to change the player frame.
__________________
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
04-13-19, 04:11 PM   #4
SilverDragony
A Murloc Raider
Join Date: Apr 2019
Posts: 6
Thank you Fizzlemizz. I've changed a little for understand, but with your code, now this the player frame wish isn't displayed. I searched how to display it, but I don't find solution.
  Reply With Quote
04-13-19, 04:21 PM   #5
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Originally Posted by SDPhantom View Post
You could achieve the same result just by having the target frame register for PLAYER_TARGET_CHANGED in addition to UNIT_PORTRAIT_UPDATE. The script should still be able to handle the event as-is. No need to change the player frame.
You are indeed correct.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
04-13-19, 04:22 PM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Originally Posted by SilverDragony View Post
Thank you Fizzlemizz. I've changed a little for understand, but with your code, now this the player frame wish isn't displayed. I searched how to display it, but I don't find solution.
You should post the new code so we can see what might be happening.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
04-13-19, 04:44 PM   #7
SilverDragony
A Murloc Raider
Join Date: Apr 2019
Posts: 6
I have this code :

Lua Code:
  1. --Player Frame
  2.     local fp = CreateFrame("Button", "FarmootPlayerPortrait", UIParent, "SecureUnitButtonTemplate")
  3.     fp:SetSize(60, 60)
  4.     fp:SetPoint("CENTER", -40, -350)
  5.     fp:SetAttribute("unit", "player")
  6.     RegisterUnitWatch(fp)
  7.     fp:SetAttribute("toggleForVehicle", true)
  8.     fp:RegisterForClicks("AnyUp")
  9.     fp:SetAttribute("*type1", "target")
  10.     fp:SetAttribute("*type2", "togglemenu")
  11.     fp:SetAttribute("*type3", "assist")
  12.     fp.Texture = fp:CreateTexture("$parent_Texture", "BACKGROUND")
  13.     fp.Texture:SetAllPoints()
  14.     SetPortraitTexture(fp.Texture, "player")
  15.     fp.Border = fp:CreateTexture("$parent_Border", "BORDER")
  16.     fp.Border:SetPoint("TOPLEFT", -6, 4)
  17.     fp.Border:SetPoint("BOTTOMRIGHT", 6, -10)
  18.     fp.Border:SetTexture("Interface/PLAYERFRAME/UI-PlayerFrame-Deathknight-Ring")
  19.     fp.Border:SetVertexColor(1, 1, 0, 1)
  20.     fp:RegisterUnitEvent("UNIT_PORTRAIT_UPDATE", "player")
  21.     fp:RegisterEvent("PLAYER_TARGET_CHANGED") -- based on suggestion of Fizzlemizz
  22.     fp:SetScript("OnEvent", function(self)
  23.         if event == "UNIT_PORTRAIT_UPDATE" or "PLAYER_TARGET_CHANGED" then -- based on suggestion of SDPhantom
  24.             SetPortraitTexture(self.Texture, "player")
  25.         else
  26.             SetPortraitTexture(self.Target.Texture, "target") -- based on suggestion of Fizzlemizz
  27.         end
  28.     end)
  29.      
  30.     --Target Frame
  31.     local ft = CreateFrame("Button", "FarmootPlayerPortrait", UIParent, "SecureUnitButtonTemplate")
  32.     fp.Target = ft -- based on suggestion of Fizzlemizz
  33.     ft:SetSize(60, 60)
  34.     ft:SetPoint("CENTER", 100, -350)
  35.     ft:SetAttribute("unit", "target")
  36.     RegisterUnitWatch(ft)
  37.     ft:SetAttribute("toggleForVehicle", true)
  38.     ft:RegisterForClicks("AnyUp")
  39.     ft:SetAttribute("*type1", "target")
  40.     ft:SetAttribute("*type2", "togglemenu")
  41.     ft:SetAttribute("*type3", "assist")
  42.     ft.Texture = ft:CreateTexture("$parent_Texture", "BACKGROUND")
  43.     ft.Texture:SetAllPoints()
  44.     SetPortraitTexture(ft.Texture, "target")
  45.     ft.Border = ft:CreateTexture("$parent_Border", "BORDER")
  46.     ft.Border:SetPoint("TOPLEFT", -6, 4)
  47.     ft.Border:SetPoint("BOTTOMRIGHT", 6, -10)
  48.     ft.Border:SetTexture("Interface/PLAYERFRAME/UI-PlayerFrame-Deathknight-Ring")
  49.     ft.Border:SetVertexColor(1, 1, 0, 1)
  50.     ft:RegisterUnitEvent("UNIT_PORTRAIT_UPDATE", "target")
  51.     ft:SetScript("OnEvent", function(self) SetPortraitTexture(self.Texture, "target")  end)

But with this code, I have player frame and not target frame^^
  Reply With Quote
04-13-19, 05:46 PM   #8
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Maybe I'm missing something (probably), but wouldn't naming both fp and ft "FarmootPlayerPortrait" –– ie: the same global name –– cause problems?

Wouldn't WoW complain that two frames with the same name exist?
  Reply With Quote
04-13-19, 05:59 PM   #9
JDoubleU00
A Firelord
 
JDoubleU00's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 463
Originally Posted by myrroddin View Post
Maybe I'm missing something (probably), but wouldn't naming both fp and ft "FarmootPlayerPortrait" –– ie: the same global name –– cause problems?

Wouldn't WoW complain that two frames with the same name exist?
Oh, you must have copied the code Fizzlemizz helped me with on the UI & Macro Forums. I'll have a look to see if I have a working copy handy.
__________________
Author of JWExpBar and JWRepBar.
  Reply With Quote
04-13-19, 06:17 PM   #10
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
WoW doesn't care if you overwrite (re-use) frame names, you'll just get the first one created using _G["FrameName"] or FrameName but using the local referernce will still differentiate them in your code.

I moved the PLAYER_TARGET_CHANGED event as per SDPhantom's suggestion down to the target frame to make it easier to read/understand ie. the Player frame doesn't now need to have a reference to the Target frame to make the change as it's all done in the Target frame.

Lua Code:
  1. --Player Frame
  2. local fp = CreateFrame("Button", "FarmootPlayerPortrait", UIParent, "SecureUnitButtonTemplate")
  3. fp:SetSize(60, 60)
  4. fp:SetPoint("CENTER", -40, -350)
  5. fp:SetAttribute("unit", "player")
  6. RegisterUnitWatch(fp)
  7. fp:SetAttribute("toggleForVehicle", true)
  8. fp:RegisterForClicks("AnyUp")
  9. fp:SetAttribute("*type1", "target")
  10. fp:SetAttribute("*type2", "togglemenu")
  11. fp:SetAttribute("*type3", "assist")
  12. fp.Texture = fp:CreateTexture("$parent_Texture", "BACKGROUND")
  13. fp.Texture:SetAllPoints()
  14. SetPortraitTexture(fp.Texture, "player")
  15. fp.Border = fp:CreateTexture("$parent_Border", "BORDER")
  16. fp.Border:SetPoint("TOPLEFT", -6, 4)
  17. fp.Border:SetPoint("BOTTOMRIGHT", 6, -10)
  18. fp.Border:SetTexture("Interface/PLAYERFRAME/UI-PlayerFrame-Deathknight-Ring")
  19. fp.Border:SetVertexColor(1, 1, 0, 1)
  20. fp:RegisterUnitEvent("UNIT_PORTRAIT_UPDATE", "player")
  21. fp:SetScript("OnEvent", function(self) SetPortraitTexture(self.Texture, "player") end)
  22.  
  23. --Target Frame
  24. local ft = CreateFrame("Button", "FarmootTargetPortrait", UIParent, "SecureUnitButtonTemplate")
  25. fp.Target = ft -- based on suggestion of Fizzlemizz
  26. ft:SetSize(60, 60)
  27. ft:SetPoint("CENTER", 100, -350)
  28. ft:SetAttribute("unit", "target")
  29. RegisterUnitWatch(ft)
  30. ft:SetAttribute("toggleForVehicle", true)
  31. ft:RegisterForClicks("AnyUp")
  32. ft:SetAttribute("*type1", "target")
  33. ft:SetAttribute("*type2", "togglemenu")
  34. ft:SetAttribute("*type3", "assist")
  35. ft.Texture = ft:CreateTexture("$parent_Texture", "BACKGROUND")
  36. ft.Texture:SetAllPoints()
  37. ft.Border = ft:CreateTexture("$parent_Border", "BORDER")
  38. ft.Border:SetPoint("TOPLEFT", -6, 4)
  39. ft.Border:SetPoint("BOTTOMRIGHT", 6, -10)
  40. ft.Border:SetTexture("Interface/PLAYERFRAME/UI-PlayerFrame-Deathknight-Ring")
  41. ft.Border:SetVertexColor(1, 1, 0, 1)
  42. ft:RegisterUnitEvent("UNIT_PORTRAIT_UPDATE", "target")
  43. ft:RegisterEvent("PLAYER_TARGET_CHANGED") -- based on suggestion of Fizzlemizz
  44. ft:SetScript("OnEvent", function(self) SetPortraitTexture(self.Texture, "target")  end)
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 04-13-19 at 06:54 PM.
  Reply With Quote
04-14-19, 01:50 PM   #11
SilverDragony
A Murloc Raider
Join Date: Apr 2019
Posts: 6
Thank you for your help. That works ! I will continue to work on this code and will go back here if I've questions.
  Reply With Quote
04-16-19, 05:06 AM   #12
SilverDragony
A Murloc Raider
Join Date: Apr 2019
Posts: 6
Hi,

I work on this code but I have another probleme. I realy liked your help on this post so I continue here.

I sursh to display a border for each classification, but I don't understand why my code doesn't work. Is there a problem is local function ?

Lua Code:
  1. --Target Classification
  2.     local default = "Interface/AddOns/ClassicPortrait/media/default"
  3.     local unitClassTextures = {
  4.         worldboss = "Interface/AddOns/ClassicPortrait/media/worldboss",
  5.         boss = "Interface/AddOns/ClassicPortrait/media/boss",
  6.         elite = "Interface/AddOns/ClassicPortrait/media/elite",
  7.         rare = "Interface/AddOns/ClassicPortrait/media/rare",
  8.         rareelite = "Interface/AddOns/ClassicPortrait/media/rareelite",
  9.     }
  10.      
  11.     local function OnEvent(self, event, ...)
  12.         if event == "PLAYER_TARGET_CHANGED" and UnitExists("target") then
  13.             self.Border:SetTexture( unitClassTextures[UnitClassification("target")] or default )
  14.         end
  15.     end
  16.      
  17.     local function CreateBorder(self)
  18.         local t = self.Border or self:CreateTexture(nil, "BACKGROUND", nil, -8)
  19.         self.Border = t
  20.      
  21.         ft:SetPoint("CENTER", 110, -350)
  22.         ft.Border = ft:CreateTexture("$parent_Border", "BORDER")
  23.         ft.Border:SetPoint("TOPLEFT", -6, 4)
  24.         ft.Border:SetPoint("BOTTOMRIGHT", 6, -10)
  25.         ft.Border:SetVertexColor(1, 1, 0, 1)
  26.      
  27.         self:RegisterEvent("PLAYER_TARGET_CHANGED")
  28.         self:HookScript("OnEvent", OnEvent)
  29.     end

I verify in game on an elite unit but the addons display nothing.

Thank again for your help and have a good day
  Reply With Quote
04-16-19, 09:56 AM   #13
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Lua Code:
  1. local function CreateBorder(self)
  2.     local t = self.Border or self:CreateTexture(nil, "BACKGROUND", nil, -8)
  3.     self.Border = t
  4.      
  5.     ft:SetPoint("CENTER", 110, -350)
  6.     ft.Border = ft:CreateTexture("$parent_Border", "BORDER")
  7.     ft.Border:SetPoint("TOPLEFT", -6, 4)
  8.     ft.Border:SetPoint("BOTTOMRIGHT", 6, -10)
  9.     ft.Border:SetVertexColor(1, 1, 0, 1)
  10.      
  11.     self:RegisterEvent("PLAYER_TARGET_CHANGED")
  12.     self:HookScript("OnEvent", OnEvent)
  13. end

As well as at local t = ..., this function also is creating a new texture for ft.Border every time it is called
Code:
ft.Border = ft:CreateTexture("$parent_Border", "BORDER")
You adjust (SetPoint), create and color/adjust again ft but not t so self.Border (t) never gets placed anywhere on screen meaning you can't see the changes. For such a small amount of code you might get rid of "t" and "ft" and just use "self".

I'm not sure why you need to use the function and test if the self.Border texture exists if this is only for the target frame. If it's being called for multiple frames, especially if you have more functions like this, you are adding more and more hooks to OnEvent each time.

You might want to think about using a single frame to process all events (no hooks) and use that to make changes to the individual unit frames as required rather than multiple event handlers possibly processing the same event(s), not to mention keeping track of which handler(s) need updating when code changes.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 04-16-19 at 01:31 PM.
  Reply With Quote
04-17-19, 04:18 AM   #14
SilverDragony
A Murloc Raider
Join Date: Apr 2019
Posts: 6
I'm learning lua, and it's not obvious for me. I sursh on plurality forums but I don't understand why y code doesn't work. Actualy I have this :

Lua Code:
  1. --Player Frame
  2.     local fp = CreateFrame("Button", "FarmootPlayerPortrait", UIParent, "SecureUnitButtonTemplate")
  3.     fp:SetSize(70, 70)
  4.     fp:SetPoint("CENTER", -80, -350)
  5.     fp:SetAttribute("unit", "player")
  6.     RegisterUnitWatch(fp)
  7.     fp:SetAttribute("toggleForVehicle", true)
  8.     fp:RegisterForClicks("AnyUp")
  9.     fp:SetAttribute("*type1", "target")
  10.     fp:SetAttribute("*type2", "togglemenu")
  11.     fp:SetAttribute("*type3", "assist")
  12.     fp.Texture = fp:CreateTexture("$parent_Texture", "BACKGROUND")
  13.     fp.Texture:SetAllPoints()
  14.     SetPortraitTexture(fp.Texture, "player")
  15.     fp.Border = fp:CreateTexture("$parent_Border", "BORDER")
  16.     fp.Border:SetPoint("TOPLEFT", -6, 4)
  17.     fp.Border:SetPoint("BOTTOMRIGHT", 6, -10)
  18.     fp.Border:SetTexture("Interface/PLAYERFRAME/UI-PlayerFrame-Deathknight-Ring")
  19.     fp.Border:SetVertexColor(1, 1, 0, 1)
  20.     fp:RegisterUnitEvent("UNIT_PORTRAIT_UPDATE", "player")
  21.     fp:SetScript("OnEvent", function(self) SetPortraitTexture(self.Texture, "player") end)
  22.      
  23.     --Target Frame
  24.     local ft = CreateFrame("Button", "FarmootTargetPortrait", UIParent, "SecureUnitButtonTemplate")
  25.     fp.Target = ft -- based on suggestion of Fizzlemizz
  26.     ft:SetSize(70, 70)
  27.     ft:SetPoint("CENTER", 110, -350)
  28.     ft:SetAttribute("unit", "target")
  29.     RegisterUnitWatch(ft)
  30.     ft:SetAttribute("toggleForVehicle", true)
  31.     ft:RegisterForClicks("AnyUp")
  32.     ft:SetAttribute("*type1", "target")
  33.     ft:SetAttribute("*type2", "togglemenu")
  34.     ft:SetAttribute("*type3", "assist")
  35.     ft.Texture = ft:CreateTexture("$parent_Texture", "BACKGROUND")
  36.     ft.Texture:SetAllPoints()
  37.     ft.Border = ft:CreateTexture("$parent_Border", "BORDER")
  38.     ft.Border:SetPoint("TOPLEFT", -6, 4)
  39.     ft.Border:SetPoint("BOTTOMRIGHT", 6, -10)
  40.     ft.Border:SetTexture("Interface/PLAYERFRAME/UI-PlayerFrame-Deathknight-Ring")
  41.     ft.Border:SetVertexColor(1, 1, 0, 1)
  42.     ft:RegisterUnitEvent("UNIT_PORTRAIT_UPDATE", "target")
  43.     ft:RegisterEvent("PLAYER_TARGET_CHANGED") -- based on suggestion of Fizzlemizz
  44.     ft:SetScript("OnEvent", function(self) SetPortraitTexture(self.Texture, "target")  end)
  45.    
  46.     --Target Classification
  47.     local default = "Interface/AddOns/ClassicPortrait/media/default"
  48.     local uClassTextures = {
  49.         worldboss = "Interface/AddOns/ClassicPortrait/media/worldboss",
  50.         boss = "Interface/AddOns/ClassicPortrait/media/boss",
  51.         elite = "Interface/AddOns/ClassicPortrait/media/elite",
  52.         rare = "Interface/AddOns/ClassicPortrait/media/rare",
  53.         rareelite = "Interface/AddOns/ClassicPortrait/media/rareelite",
  54.     }
  55.     local uClass = UnitClassification("target")
  56.     local function OnEvent(self, event, ...)
  57.         if event == "PLAYER_TARGET_CHANGED" and UnitExists("target") then
  58.             self:SetTexture( unitClassTextures[UnitClassification("target")] or default )
  59.         end
  60.         self:RegisterEvent("PLAYER_TARGET_CHANGED")
  61.     end

This display portraits but not classification textutes. I deleted settings and keep those in --Target Frame. i try to follow your advice, but it not easy to write the code.
  Reply With Quote
04-17-19, 10:56 AM   #15
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Your code above, the target frame (ft) is assigned an OnEvent script using:
Code:
ft:SetScript("OnEvent", function(self) SetPortraitTexture(self.Texture, "target")  end)
It didn't ever assign the OnEvent function (which would have required using a :HookScript or else it would have only replaced (overwritten) the original script that set the portrait texture if you had used SetScript).

This is an example using a single event frame (EventFrame) for all units rather than an OnEvent script for each unit frame. The separate event frame is not required eg. you could use the Player frame for all events, it just helps to make clearer what is happening.

You can add events to OnEventFunc and use it to update the units needed for that event.

Lua Code:
  1. --Target Classification
  2. local default = "Interface/AddOns/ClassicPortrait/media/default"
  3. local uClassTextures = {
  4.     worldboss = "Interface/AddOns/ClassicPortrait/media/worldboss",
  5.     boss = "Interface/AddOns/ClassicPortrait/media/boss",
  6.     elite = "Interface/AddOns/ClassicPortrait/media/elite",
  7.     rare = "Interface/AddOns/ClassicPortrait/media/rare",
  8.     rareelite = "Interface/AddOns/ClassicPortrait/media/rareelite",
  9. }
  10.  
  11. local UnitFrames = {} -- holder for unit frames created, Stored as UnitFrames[unit id] = frame
  12.  
  13. local fp = CreateFrame("Button", "FarmootPlayerPortrait", UIParent, "SecureUnitButtonTemplate")
  14. UnitFrames.player = fp -- add the player frame to the UnitFrames table
  15. fp:SetSize(70, 70)
  16. fp:SetPoint("CENTER", -80, -350)
  17. fp:SetAttribute("unit", "player")
  18. RegisterUnitWatch(fp)
  19. fp:SetAttribute("toggleForVehicle", true)
  20. fp:RegisterForClicks("AnyUp")
  21. fp:SetAttribute("*type1", "target")
  22. fp:SetAttribute("*type2", "togglemenu")
  23. fp:SetAttribute("*type3", "assist")
  24. fp.Texture = fp:CreateTexture("$parent_Texture", "BACKGROUND")
  25. fp.Texture:SetAllPoints()
  26. --SetPortraitTexture(fp.Texture, "player")
  27. fp.Border = fp:CreateTexture("$parent_Border", "BORDER")
  28. fp.Border:SetPoint("TOPLEFT", -6, 4)
  29. fp.Border:SetPoint("BOTTOMRIGHT", 6, -10)
  30. fp.Border:SetTexture("Interface/PLAYERFRAME/UI-PlayerFrame-Deathknight-Ring")
  31. fp.Border:SetVertexColor(1, 1, 0, 1)
  32.  
  33. --Target Frame
  34. local ft = CreateFrame("Button", "FarmootTargetPortrait", UIParent, "SecureUnitButtonTemplate")
  35. UnitFrames.target = ft -- add the target frame to the UnitFrames table
  36. ft:SetSize(70, 70)
  37. ft:SetPoint("CENTER", 110, -350)
  38. ft:SetAttribute("unit", "target")
  39. RegisterUnitWatch(ft)
  40. ft:SetAttribute("toggleForVehicle", true)
  41. ft:RegisterForClicks("AnyUp")
  42. ft:SetAttribute("*type1", "target")
  43. ft:SetAttribute("*type2", "togglemenu")
  44. ft:SetAttribute("*type3", "assist")
  45. ft.Texture = ft:CreateTexture("$parent_Texture", "BACKGROUND")
  46. ft.Texture:SetAllPoints()
  47. ft.Border = ft:CreateTexture("$parent_Border", "BORDER")
  48. ft.Border:SetPoint("TOPLEFT", -6, 4)
  49. ft.Border:SetPoint("BOTTOMRIGHT", 6, -10)
  50. ft.Border:SetVertexColor(1, 1, 0, 1)
  51.  
  52. local function OnEventFunc(self, event, ...)
  53.     local arg1 = ... -- add more args depending returns from on events used. UNIT_xxx events return the unit id ("player", "target" etc.) as arg1 (being first parameter of ...).
  54.     -- UnitFrames[arg1] eg. UnitFrames["player"]
  55.     if event == "UNIT_PORTRAIT_UPDATE" and UnitFrames[arg1] then
  56.         SetPortraitTexture(UnitFrames[arg1].Texture, arg1)
  57.     elseif event == "PLAYER_TARGET_CHANGED" and UnitExists("target") then
  58.         SetPortraitTexture(UnitFrames.target.Texture, "target")
  59.         UnitFrames.target.Border:SetTexture(uClassTextures[UnitClassification("target")] or default)
  60.     end
  61. end
  62. local EventFrame = CreateFrame("Frame")
  63. EventFrame:SetScript("OnEvent", OnEventFunc)
  64. EventFrame:RegisterEvent("UNIT_PORTRAIT_UPDATE")
  65. EventFrame:RegisterEvent("PLAYER_TARGET_CHANGED")
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 04-17-19 at 02:01 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Create target 2D portrait

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