WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Lua Event for Target of target Change, Nameplate addon (https://www.wowinterface.com/forums/showthread.php?t=59862)

grog162 04-29-24 08:53 AM

Lua Event for Target of target Change, Nameplate addon
 
TL/DR
I am looking for a Lua:Event that gets triggered when Target of target changes.

What am I trying to achieve?
I am making a UI that has no Unit Frames (edit: or action bars), only Nameplates. All the functionality I can get with WA and Nameplate addons. The only thing I'm missing is a Target of target indicator on my Target's nameplate.

What have I tried so far?
I have written a small addon that does show Target of target correctly. But it doesn't update whenever Target of target changes. I couldn't find the event that gets triggered by tot-change on Wowpedia or the forums.
Here's the code I've got so far:
Lua Code:
  1. local npToT = CreateFrame("Frame", NameplateToT, UIParent);
  2. local font = "Interface\\Addons\\NameplateTargetOfTarget\\Rubik-Medium.ttf";
  3.  
  4. npToT.text = npToT:CreateFontString(nil, "OVERLAY");
  5.  
  6. local function npToT_EventHandler(self, event, ...)
  7.     self.text:ClearAllPoints();
  8.     if (C_NamePlate.GetNamePlateForUnit("target") and UnitExists("targettarget")) then
  9.         local targetnameplate = C_NamePlate.GetNamePlateForUnit("target");
  10.         self.text:Show();
  11.         self.text:SetSize(100, 20);
  12.         self.text:SetFont(font, 12, "OUTLINE");
  13.         self.text:SetFormattedText(UnitName("targettarget"))
  14.         self.text:SetPoint("CENTER", targetnameplate, "CENTER", -40, -10);
  15.     else
  16.         self.text:Hide();
  17.     end
  18. end
  19.  
  20. npToT:SetScript("OnEvent", npToT_EventHandler);
  21.  
  22. -- Which events do I need to add here?
  23. npToT:RegisterEvent("ADDON_LOADED");
  24. npToT:RegisterEvent("PLAYER_ENTERING_WORLD");
  25. npToT:RegisterEvent("PLAYER_TARGET_CHANGED");
It would be nice to work both in and out of combat, for friendly and hostile targets.

What are some alternative solutions to the problem?
I know Plater can show target of a unit when casting, but I'd like to have it show tot at all times.

TL/DR at the top of the post.

Fizzlemizz 04-29-24 09:47 AM

UNIT_TARGET

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.     self.text:SetFormattedText(UnitName("targettarget"))
  17.     self.text:ClearAllPoints();
  18.     self.text:SetPoint("CENTER", C_NamePlate.GetNamePlateForUnit("target"), "CENTER", -40, -10);
  19.     self.text:Show();
  20. end
  21.  
  22. npToT:SetScript("OnEvent", npToT_EventHandler);
  23.  
  24. npToT:RegisterEvent("PLAYER_ENTERING_WORLD");
  25. npToT:RegisterEvent("UNIT_TARGET");

grog162 04-29-24 02:11 PM

Quote:

Originally Posted by Fizzlemizz (Post 343806)

I should have checked it. Noted. Thank you.
The text on wiki is misleading.
Quote:

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.

Fizzlemizz 04-29-24 02:37 PM

Any event starting with UNIT_ should work for any valid unitid.


Quote:

Originally Posted by grog162 (Post 343807)
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.

Code:

local arg1, arg2, arg3 = SomeFuncWith3Returns()
-- get all returns

local _, _, arg3 = SomeFuncWith3Returns()
-- get 3rd return

local retVal = select(2, SomeFuncWith3Returns())
-- get 2nd return (same but different to no.2 (will actually return 2 AND any others)


grog162 04-29-24 03:04 PM

Quote:

Originally Posted by Fizzlemizz (Post 343808)
Any event starting with UNIT_ should work for any valid unitid.

I thought that as well, but it had a delay for whatever reason.

Thank you for all the help.


All times are GMT -6. The time now is 09:54 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI