Quantcast How to make this Lua fire upon entering arena? - WoWInterface
Thread Tools Display Modes
05-27-18, 10:43 AM   #1
Xancepants
A Deviate Faerie Dragon
Join Date: Oct 2014
Posts: 15
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!
  Reply With Quote
05-27-18, 10:53 AM   #2
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,178
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.
__________________
  Reply With Quote
05-27-18, 11:08 AM   #3
Xancepants
A Deviate Faerie Dragon
Join Date: Oct 2014
Posts: 15
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.
  Reply With Quote
05-27-18, 11:26 AM   #4
Xancepants
A Deviate Faerie Dragon
Join Date: Oct 2014
Posts: 15
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?
  Reply With Quote
05-27-18, 12:01 PM   #5
Ammako
A Cobalt Mageweaver
 
Ammako's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 243
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.)
__________________
█████████████
█████████████
█████████████
█████████████
  Reply With Quote
05-27-18, 12:06 PM   #6
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,178
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
__________________
  Reply With Quote
05-27-18, 12:42 PM   #7
Ammako
A Cobalt Mageweaver
 
Ammako's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 243
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)
__________________
█████████████
█████████████
█████████████
█████████████

Last edited by Ammako : 05-27-18 at 12:54 PM.
  Reply With Quote
05-27-18, 12:54 PM   #8
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,178
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.
__________________
  Reply With Quote
06-09-18, 06:07 PM   #9
Theroxis
A Fallenroot Satyr
Join Date: Jun 2018
Posts: 24
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.

Last edited by Theroxis : 06-09-18 at 06:21 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » How to make this Lua fire upon entering arena?

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off