View Single Post
04-24-20, 05:36 AM   #5
Xruptor
A Flamescale Wyrmkin
 
Xruptor's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2005
Posts: 137
Originally Posted by sezz View Post
friendly nameplates are protected in instances/pvp
Sezz pretty much hit it on the nail. You can't modify nameplates in instances or pvp, if you try to do so you will trigger taint errors and such. Blizzard did this to prevent nameplates from being used to calculate positioning and what not and other things they didn't want folks doing.

Outside of that scope you can modify them as you like. A question though? Why not just use the colored healthbars to know the class? It shouldn't be necessary to place a class icon. Even if you are color blind there are settings that can adjust that.

Otherwise you if you want to use that code you need to add measures to prevent errors.


Code:
local iconKey = addonName .. "Icon"

local GetNamePlateForUnit = C_NamePlate.GetNamePlateForUnit

local iconTexture = {
    ["DEATHKNIGHT"] = 135771,
    ["DEMONHUNTER"] = 236415,
    ["DRUID"] = 625999,
    ["HUNTER"] = 626000,
    ["MAGE"] = 626001,
    ["MONK"] = 626002,
    ["PALADIN"] = 626003,
    ["PRIEST"] = 626004,
    ["ROGUE"] = 626005,
    ["SHAMAN"] = 626006,
    ["WARLOCK"] = 626007,
    ["WARRIOR"] = 626008
}

local frame = CreateFrame("Frame")
 
frame:SetScript("OnEvent", function(self, event, unit)
	local inInstance, instanceType = IsInInstance()
	
	--make sure we aren't in an instance
	if not inInstance then
		--next check to see if you can even TOUCH the namePlate, using CanAccessObject
		local namePlate = GetNamePlateForUnit(unit)
		if namePlate and CanAccessObject(namePlate) then
	
			if event == "NAME_PLATE_UNIT_ADDED" and UnitIsFriend("player", unit) then
				local _, class = UnitClass(unit)
				if iconTexture[class] then
					local icon = namePlate[iconKey]
					if not icon then
						icon = namePlate:CreateTexture(nil, "OVERLAY")
						icon:SetPoint('CENTER', 0, 22)
						icon:SetSize(22, 22)
						namePlate[iconKey] = icon
					end
					icon:SetTexture(iconTexture[class])
					icon:Show()
					return
				end
			end
			if namePlate[iconKey] then
				namePlate[iconKey]:Hide()
			end
		
		end
	end
end)

frame:RegisterEvent("NAME_PLATE_UNIT_ADDED")
frame:RegisterEvent("NAME_PLATE_UNIT_REMOVED")
__________________
Click HERE for the ultimate idiot test.

if (sizeof(sadness) > sizeof(happiness)) { initDepression(); }
  Reply With Quote