View Single Post
08-21-18, 07:55 AM   #1
Mitur
A Murloc Raider
Join Date: Aug 2008
Posts: 6
Class icons on nameplates

Hi guys, first timer here!
Did some search on these forums and discovered a lot of useful information, you are really helpful bunch of people!
Trying to figure out the nameplate magics.
For practice I'm attempting to show class icons over the friendly nameplates.
After the initial loading things work well, but after I move my camera and nameplates get dynamically reassigned, icons tend to appear over wrong classes.
To narrow things down I've tried to restrict the icons to WARRIOR class only.
And it obviously didn't work as planned
https://imgur.com/G3UPLIx

Lua Code:
  1. local events = {
  2.     ["npu_added"] = "NAME_PLATE_UNIT_ADDED",
  3.     ["npu_removed"] = "NAME_PLATE_UNIT_REMOVED",
  4.     -- ["bg"] = "PLAYER_ENTERING_BATTLEGROUND",
  5.     -- ["gru"] = "GROUP_ROSTER_UPDATE",
  6.     -- ["ptc"] = "PLAYER_TARGET_CHANGED",
  7.     ["load"] = "ADDON_LOADED"
  8. }
  9. local icons = {
  10.     ["DEMONHUNTER"] = 236415,
  11.     ["DRUID"] = 625999,
  12.     ["HUNTER"] = 626000,
  13.     ["MAGE"] = 626001,
  14.     ["MONK"] = 626002,
  15.     ["PALADIN"] = 626003,
  16.     ["PRIEST"] = 626004,
  17.     ["ROGUE"] = 626005,
  18.     ["SHAMAN"] = 626006,
  19.     ["WARLOCK"] = 626007,
  20.     ["WARRIOR"] = 626008,
  21.     ["DEATHKNIGHT"] = 135771
  22. }
  23.  
  24. --[[ Potentially useful checks
  25.     UnitIsFriend(unit, otherUnit) comparing relationships between units
  26.     UnitIsPlayer(unit)
  27.     UnitInPArty(unit)
  28. ]]
  29.  
  30. local guidSet = {}
  31. local idSet = {}
  32.  
  33. local sdb = CreateFrame("Frame")
  34. local _G = _G
  35. local GetNamePlateForUnit = _G.C_NamePlate.GetNamePlateForUnit --get nameplate from global UI variable _G
  36.  
  37. local function addIcon(parentFrame, icon)
  38.     local iconFrame, iconTexture
  39.     iconFrame = CreateFrame("Frame", nil, parentFrame)
  40.     iconFrame:SetFrameStrata("BACKGROUND")
  41.     iconFrame:SetWidth(32)
  42.     iconFrame:SetHeight(32)
  43.  
  44.     iconTexture = iconFrame:CreateTexture(nil, "BACKGROUND")
  45.     iconTexture:SetTexture(icons[icon])
  46.     iconTexture:SetAllPoints(iconFrame)
  47.  
  48.     iconFrame.texture = iconTexture
  49.     iconFrame:SetPoint("TOPLEFT", 10, 10)
  50.     iconFrame:Show()
  51. end
  52.  
  53. local function removeIcon(parentFrame)
  54.     parentFrame:Hide()
  55. end
  56.  
  57. local function nameplateHandler(self, event, unit)
  58.     local namePlate = GetNamePlateForUnit(unit)
  59.     local unit_frame = namePlate.UnitFrame
  60.     local unitGUID = UnitGUID(unit)
  61.     local separator = ", "
  62.     local className, classId, raceName, raceId, gender, name, realm = GetPlayerInfoByGUID(unitGUID)
  63.     if event == events["npu_added"] then
  64.         if classId == "WARRIOR" and not idSet:contains(unit) then
  65.             print(name .. separator .. classId .. separator .. unit .. " added!")
  66.             idSet[unit] = name
  67.             addIcon(namePlate, classId)
  68.         end
  69.     elseif event == events["npu_removed"] and idSet:contains(unit) then
  70.         print("Was saved as " .. idSet[id])
  71.         print("Was removed as " .. name)
  72.         idSet:remove(unit)
  73.         removeIcon(namePlate)
  74.     end
  75. end
  76.  
  77. local function showFriendlyNameplates(self, event)
  78.     if event == events["load"] then
  79.         SetCVar("nameplateShowAll", 1)
  80.         SetCVar("nameplateShowFriends", 1)
  81.     end
  82. end
  83.  
  84. -- Utility functions
  85.  
  86. function guidSet:contains(key)
  87.     return self[key] ~= nil
  88. end
  89.  
  90. function idSet:contains(key)
  91.     return self[key] ~= nil
  92. end
  93.  
  94. function idSet:remove(key)
  95.     idSet[key] = nil
  96. end
  97.  
  98. -- Registering for a table of events
  99.  
  100. for k, v in pairs(events) do
  101.     sdb:RegisterEvent(v)
  102. end
  103. sdb:SetScript("OnEvent", nameplateHandler)
  104. -- sdb:SetScript("OnEvent", showFriendlyNameplates)

I feel that the removeIcon function is faulty as I don't know how to get to the frame I've created on top of the nameplate.
Maybe I have to gather all the frames I create in a table?
Or perhaps all the stuff I do is just plain wrong and there is a simpler way to accomplish this task that I don't see as a beginner.
Thank you very much for taking your time to read this and for any hints and advice!

Last edited by Mitur : 08-22-18 at 03:10 AM.
  Reply With Quote