WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   How to make this Lua fire upon entering arena? (https://www.wowinterface.com/forums/showthread.php?t=56245)

Xancepants 05-27-18 10:43 AM

How to make this Lua fire upon entering arena?
 
Hello all!
I found this bit of Lua, which converts an Enemies Name on their nameplate to be their Arena# instead. When I put it in a /run macro and click it once I've entered the arena instance it works fine. However, when I remove the /run and put just the code in a Lua addon file, it doesn't work. I'm guessing it needs some sort of event to trigger it, such as when entering the arena?


Here is the Lua:

Lua Code:
  1. local U=UnitIsUnit hooksecurefunc("CompactUnitFrame_UpdateName",function(F)if IsActiveBattlefieldArena()and F.unit:find("nameplate")then for i=1,5 do if U(F.unit,"arena"..i)then F.name:SetText(i)F.name:SetTextColor(1,1,0)break end end end end)


It doesn't throw an errors or anything, just doesn't have any code to make it fire on anything I think. Any help is appreciated, and thank you in advance! :)

Xrystal 05-27-18 10:53 AM

have you tried displaying a message before the if statement to see if the function is called? Seeing as you are simply hooking into an existing function I would expect it to run when that function is called by Blizzard. As long as your addon has your lua file listed in the toc file it should execute fine.

Xancepants 05-27-18 11:08 AM

Yea it's listed in the .toc file alright... But just FYI I'm super novice with Lua, so a lot of what you said went way over my head heh... I thought the same thing, it should just work, right? Everything else that had a /run, I've dropped in a Lua file and removed the /run and it worked. Anywho, I'm not sure at all how to go about trying out what you've suggested, still very much a beginner.

Xancepants 05-27-18 11:26 AM

One thing to add... I just ran an arena skirmish and they had Arena#'s instead of names... and I didn't change anything to the Lua??? I guess sometimes it works, and sometimes it doesn't? :confused:

Ammako 05-27-18 12:01 PM

This is one of those things I told you about where if you have multiple hooks/functions that edit the same thing, you may end up with conflicts like this where code may fight over who gets to apply their change (in your case, the game has code trying to apply a name without the appended realm name but another bit of code hooked to the same function attempts to change the name with their arena ID at the same time.)

You'll have to edit your existing hook to add this new clause in, and prevent this from happening. i.e. add logic that checks if you are in an arena, and if you are, then names get changed to arena ID, otherwise they get changed to name w/ server name stripped out.

(Note, to help more people spot potential issues like this, it is best to show the whole code for your addon, so that others can have a better understanding of context and why something may be happening.)

Xrystal 05-27-18 12:06 PM

Sounds like it is linked to when the Blizzard Addon is loaded and you make your change, similar to your other query. If you haven't already tried it, use the ADDON_LOADED event to ensure the addon is loaded before you make your changes. The way I do it in lua for simple event watching is as follows.

Lua Code:
  1. local frame = CreateFrame("Frame")
  2. frame:RegisterEvent("ADDON_LOADED")
  3. frame:SetScript("OnEvent", function(self,event,...)
  4.      local args = {...}
  5.      if event == "ADDON_LOADED" and args[0] == "Blizzard_ArenaUI" then
  6.         -- Make any changes you want here
  7.      end
  8. end

Ammako 05-27-18 12:42 PM

I don't think it's related to Blizzard ArenaUI, they are changing the name on the default nameplates, according to the unit ID (which I assume would still be arena1/arena2/arena3/etc. regardless of ArenaUI being loaded.)

CompactUnitFrame is not an addon, so you can't really check for it with ADDON_LOADED, and Blizzard_NamePlates is not load on demand, so I would hope that this is already loaded by the time player-made addons are loaded (I don't think this would matter anyway; if nameplates weren't loaded, the code does not edit anything nameplate-specific anyway. It just happens to also work on nameplates, when those are present.)

I'm pretty sure the issue was that they had this code running concurrently, and both hooks may have been fighting over who gets to apply the change to the nameplate name:

lua Code:
  1. hooksecurefunc("CompactUnitFrame_UpdateName", function(frame)
  2.     if not frame:IsForbidden() then
  3.         local name, _ = UnitName(frame.unit)
  4.         frame.name:SetText(name)
  5.     end
  6. end)

For readability purposes here is the same code from the OP, re-factored and indented:

lua Code:
  1. hooksecurefunc("CompactUnitFrame_UpdateName", function(frame)
  2.     if IsActiveBattlefieldArena() and frame.unit:find("nameplate") then
  3.         for i=1,5 do
  4.             if UnitIsUnit(frame.unit,"arena"..i) then
  5.                 frame.name:SetText(i)
  6.                 frame.name:SetTextColor(1,1,0)
  7.                 break
  8.             end
  9.         end
  10.     end
  11. end)

Xrystal 05-27-18 12:54 PM

I stand corrected. Thanks for clarifying. I think I may have made the assumption it was all connected to the Arena addon due to the other query posted, and thought that the two queries may have been messing things up.

Theroxis 06-09-18 06:07 PM

I would use the "ARENA_PREP_OPPONENT_SPECIALIZATIONS" event. There's no event that says "HEY I ZONED INTO AN ARENA" so this is the next best thing
Code:

local Frame = CreateFrame("Frame")
Frame:RegisterEvent("ARENA_PREP_OPPONENT_SPECIALIZATIONS")

Frame:SetScript("OnEvent", function(...)
        --Your code goes here:

end)

This will restrict that code to only running once the Preperation buff has been applied to your character, which *should* be sufficient.
If not, you could then also check to see if the blizzard frame is up on an interval, but I think the above is sufficient.

EDIT:
This won't run code until the enemy spec has been populated, so it should almost certainly work.


All times are GMT -6. The time now is 08:38 AM.

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