Thread Tools Display Modes
10-22-18, 10:07 AM   #1
Pobre
A Murloc Raider
 
Pobre's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2013
Posts: 5
UnitAura returning nil

Hello guys, I asked this in IRC and I intend to post the solution (if any) when it comes.

So I was doing a simple addon to track my buffs and I noticed that sometimes UnitAura returned nil values even when the buffs are up. It is more noticeable with frequent auras occurring. I have a sample code that worked in test. It's for warrior but you can change to any other aura.

Lua Code:
  1. /run f = CreateFrame("Frame", nil, nil) f:RegisterEvent("UNIT_AURA", "player") f:SetScript("OnEvent", function(self, event, unit) print(AuraUtil.FindAuraByName("Overpower", unit)) end)

As I said, the more UNIT_AURA fires, the best you can see what I mean. Maybe I'm doing something wrong but I can't see where.

By the way, I'm posting the lua file that I noticed the problem.
Lua Code:
  1. local TEXTURE = "Interface\\ChatFrame\\ChatFrameBackground"
  2. local PIXEL = {
  3.     edgeFile = TEXTURE,
  4.     edgeSize = -1
  5. }
  6.  
  7. -- Create simple StatusBar
  8. local function CreateStatusBar(parent, name, width, height, minValue, maxValue, r, g, b)
  9.     local bar = CreateFrame("StatusBar", name, parent)
  10.     bar:SetStatusBarTexture(TEXTURE)
  11.     bar:SetBackdrop(PIXEL)
  12.     bar:SetBackdropBorderColor(0, 0, 0)
  13.     bar:SetStatusBarColor(r or 0, b or 1, g or 0)
  14.     bar:SetMinMaxValues(minValue, maxValue)
  15.     bar:SetWidth(width)
  16.     bar:SetHeight(height)
  17.  
  18.     return bar
  19. end
  20. local function Warrior()
  21.     -- Overpower bars
  22.     local opEvent = CreateFrame("Frame", nil, nil)
  23.     local op1 = CreateStatusBar(PlayerFrame, "gwOp1", 10, 5, 0, 1, .5, .5, .5)
  24.     local op2 = CreateStatusBar(PlayerFrame, "gwOp2", 10, 5, 0, 1, .5, .5, .5)
  25.     op2:SetPoint("BOTTOMLEFT", PlayerFrame.healthbar, "TOPLEFT", 0, 1)
  26.     op1:SetPoint("LEFT", gwOp2, "RIGHT", 1, 0)
  27.     op1:SetMinMaxValues(0, 1)
  28.     op2:SetMinMaxValues(1, 2)
  29.     opEvent:RegisterEvent("UNIT_AURA", "player")
  30.     opEvent.startTime = 0
  31.     opEvent.endTime = 0
  32.     op1:SetValue(0)
  33.     op2:SetValue(0)
  34.     opEvent:SetScript("OnEvent", function(self, event, unit)
  35.         local name, _, stack, _, duration, endTime = AuraUtil.FindAuraByName("Overpower", unit)
  36.         if not endTime then
  37.             op1:SetValue(0)
  38.             op2:SetValue(0)
  39.         elseif opEvent.endTime ~= endTime then
  40.             op1:SetValue(stack)
  41.             op2:SetValue(stack)
  42.             opEvent.endTime = endTime
  43.         end
  44.         -- frame test: /run f = CreateFrame("Frame", nil, nil) f:RegisterEvent("UNIT_AURA", "player") f:SetScript("OnEvent", function(self, event, unit) print(AuraUtil.FindAuraByName("Overpower", unit)) end)
  45.     end)
  46. end
  47.  
  48. -- Load auras
  49. hooksecurefunc("PlayerFrame_ToPlayerArt", function(self)
  50.     Warrior()
  51. end)

Solution:
LUA Code:
  1. opEvent:RegisterUnitEvent("UNIT_AURA", "player")

Last edited by Pobre : 10-22-18 at 02:21 PM.
  Reply With Quote
10-22-18, 12:52 PM   #2
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Change RegisterEvent to RegisterUnitEvent. You’re getting nil because it’s firing for every UNIT_AURA instead of just yours.
  Reply With Quote
10-22-18, 01:35 PM   #3
Pobre
A Murloc Raider
 
Pobre's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2013
Posts: 5
Yes, you're right.

Edit: I found that where I got the RegisterEvent with Unit is deprecated. Anyway thank you for that and making me find the patch notes (somewhere) that it became deprecated.

Last edited by Pobre : 10-22-18 at 02:20 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » UnitAura returning nil

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