Originally Posted by Fizzlemizz
|
I should have checked it.
Noted. Thank you.
The text on wiki is misleading.
Fired when the target of yourself, raid, and party members change
...
Should also work for 'pet' and 'focus'. This event only fires when the triggering unit is within the player's visual range.
|
It makes no mention of Target of target change, that's why I dismissed it.
Anyway, for the sake of completeness, here's the code I landed on.
Lua Code:
local npToT = CreateFrame("Frame", nil, UIParent);
local font = "Interface\\Addons\\NameplateTargetOfTarget\\Rubik-Medium.ttf";
npToT.text = npToT:CreateFontString(nil, "OVERLAY");
npToT.text:SetFont(font, 12, "OUTLINE"); -- Only need to set the font once
-- npToT.Text will automatically resize to fit text
local function npToT_EventHandler(self, event, ...)
if not UnitExists("targettarget") then
self.text:Hide();
return
end
local unit = ...
if not unit == "target" then
return
end
if not C_NamePlate.GetNamePlateForUnit("target") then
-- If player target doesn't have nameplate showing, the text shows at the center of the screen
self.text:Hide(); -- or displayes it on other nameplates, if target is behind you, for example.
return
end
if UnitIsPlayer("targettarget") then
local _, totclassfile = UnitClass("targettarget");
local _, _, _, totclasscolor = GetClassColor(totclassfile);
self.text:SetFormattedText("\124c%s%s\124r", totclasscolor, UnitName("targettarget"));
-- Makes the names of player units class colored.
else
self.text:SetFormattedText(UnitName("targettarget"));
end
self.text:ClearAllPoints();
self.text:SetPoint("CENTER", C_NamePlate.GetNamePlateForUnit("target"), "CENTER", -40, -10);
self.text:Show();
end
npToT:SetScript("OnEvent", npToT_EventHandler);
npToT:RegisterEvent("PLAYER_ENTERING_WORLD");
npToT:RegisterEvent("PLAYER_TARGET_CHANGED");
npToT:RegisterEvent("UNIT_TARGET");
npToT:RegisterEvent("NAME_PLATE_UNIT_ADDED");
I had a delay when I was changing targets, so I put back
PLAYER_TARGET_CHANGED.
NAME_PLATE_UNIT_ADDED is for when target nameplate appears as you target a unit or after.
Btw, I don't know how to get a specific output from a function that outputs multiple values. If someone could tell me the syntax for that, it would be much appreciated.