View Single Post
04-29-24, 02:11 PM   #3
grog162
A Defias Bandit
Join Date: Apr 2024
Posts: 3
Originally Posted by Fizzlemizz View Post
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:
  1. local npToT = CreateFrame("Frame", nil, UIParent);
  2. local font = "Interface\\Addons\\NameplateTargetOfTarget\\Rubik-Medium.ttf";
  3. npToT.text = npToT:CreateFontString(nil, "OVERLAY");
  4. npToT.text:SetFont(font, 12, "OUTLINE"); -- Only need to set the font once
  5. -- npToT.Text will automatically resize to fit text
  6.  
  7. local function npToT_EventHandler(self, event, ...)
  8.     if not UnitExists("targettarget") then
  9.         self.text:Hide();
  10.         return
  11.     end
  12.     local unit = ...
  13.     if not unit == "target" then
  14.         return
  15.     end
  16.     if not C_NamePlate.GetNamePlateForUnit("target") then
  17.         -- If player target doesn't have nameplate showing, the text shows at the center of the screen
  18.         self.text:Hide(); -- or displayes it on other nameplates, if target is behind you, for example.
  19.         return
  20.     end
  21.     if UnitIsPlayer("targettarget") then
  22.         local _, totclassfile = UnitClass("targettarget");
  23.         local _, _, _, totclasscolor = GetClassColor(totclassfile);
  24.         self.text:SetFormattedText("\124c%s%s\124r", totclasscolor, UnitName("targettarget"));
  25.         -- Makes the names of player units class colored.
  26.     else
  27.         self.text:SetFormattedText(UnitName("targettarget"));
  28.     end
  29.     self.text:ClearAllPoints();
  30.     self.text:SetPoint("CENTER", C_NamePlate.GetNamePlateForUnit("target"), "CENTER", -40, -10);
  31.     self.text:Show();
  32. end
  33.  
  34. npToT:SetScript("OnEvent", npToT_EventHandler);
  35.  
  36. npToT:RegisterEvent("PLAYER_ENTERING_WORLD");
  37. npToT:RegisterEvent("PLAYER_TARGET_CHANGED");
  38. npToT:RegisterEvent("UNIT_TARGET");
  39. 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.
  Reply With Quote