Thread Tools Display Modes
01-19-15, 10:05 AM   #1
CKlausi
A Murloc Raider
 
CKlausi's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2010
Posts: 4
Best way to scan unit auras

Hi,

my goal is to create an dottracker for my addon.
I am searching for an efficient way to scan my targets auras without creating too much garbage.
So I am using the OnUpdate function to call my "scanner" function, which looks like this

Code:
local auras = {}
local function GetAuras(unit, helpOrHarm)
	local auraIndex = 0
	for index = 1, 40 do
		local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellID = UnitAura(unit, index, helpOrHarm)
		if not name then break end
	
		auraIndex = auraIndex + 1
		auras[auraIndex ] = {} -- this is surely my memory problem
		auras[auraIndex ].spellID = spellID
                -- etc
	end
	
	return auras
end
I think this part auras[auraIndex ] = {} creates a lot of garbage.
Perhaps also my call in OnUpdate which looks like this
Code:
local aurasTarget = {}
local function OnUpdate(self, elapsed)
	timeelapsed = timeelapsed + elapsed
	if(timeelapsed > 0.05) then
		aurasTarget = UpdateAuras("target", "HARMFUL")
		timeelapsed = 0
	end
end
Memory will increase very fast. I am a little confused how to do this right, read a lot of code of other addons, but I don't find the right point
Perhaps someone can give me a point into the right direction how to get started with this

Best regards
Clausi
  Reply With Quote
01-19-15, 10:12 AM   #2
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Yes, that table is most likely the culprit, but you should also not be using OnUpdate to do these things, and instead use the UNIT_AURA event. You could probably use OnUpdate to update any timers you might have, though. Or maybe people use animations for these things, not sure.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
01-19-15, 12:34 PM   #3
jeruku
A Cobalt Mageweaver
 
jeruku's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 223
Your memory problem is definitely that. In your code you are creating 40 new tables every time you call GetAuras; tables are not immediately collected by the garbage collector meaning they remain in memory until the collector can do something with them.

Use the following method:
Lua Code:
  1. -- Looks for an existing table or creates a new one.
  2.     auras[ auraIndex ] = auras[ auraIndex ] or {}
which is short for
Lua Code:
  1. if not auras[ auraIndex ] then
  2.     auras[ auraIndex ] = {}
  3. end


As for the other part UNIT_AURA is where you should rescan auras and update them. You can always add a Cooldown frame over the top of an aura allowing the easy display of a spiral and timer.

So it would include something like the following.

Lua Code:
  1. local auras -- decalre auras outside and use it for your entire addon.
  2.  
  3.  
  4. local function GetAuras(unit, helpOrHarm)
  5. -- code
  6.     for index = 1, 40 do
  7.         -- code to get/create aurabutton among other things
  8.         if not aurabutton.cooldown then
  9.             local cooldown = CreateFrame('Cooldown', nil, aurabutton, "CooldownFrameTemplate")
  10.             cooldown:SetAllPoints(aurabutton) -- make it cover the aura
  11.             aurabutton.cooldown = cooldown -- make it easier to access elsewhere
  12.         end
  13.         aurabutton.cooldown:SetCooldown((expirationTime - duration), duration)
  14.         -- do some more stuff
  15.     end
  16. -- more code
  17. end

If this is not what you are looking for please elaborate and I will be glad to assist you further.
__________________
"I have not failed, I simply found 10,000 ways that did not work." - Thomas Edison

Last edited by jeruku : 01-20-15 at 10:28 AM. Reason: Tables and frames are NOT the same thing. Phanx & Banknorris pointed this out.
  Reply With Quote
01-20-15, 06:16 AM   #4
CKlausi
A Murloc Raider
 
CKlausi's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2010
Posts: 4
Thank you guys, that really helped me to continue
  Reply With Quote
01-20-15, 06:58 AM   #5
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
Originally Posted by jeruku View Post
tables are not and cannot be deleted from memory until reload or exiting game.
Really? I read somewhere (don't really remember) that Lua garbage collection worked in WoW when you are out of combat but stopped when you were in combat.

Wait I found it:
http://us.battle.net/wow/en/forum/topic/4038616243#2

I know FRAMES (which are tables, but not all tables are frames) can't be deleted until reload or exiting game.

Last edited by Banknorris : 01-20-15 at 07:10 AM.
  Reply With Quote
01-20-15, 08:10 AM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Yes, tables can be garbage-collected, but userdata (the "magic" part of a frame) cannot. However, creating 40 tables in an OnUpdate function is still a massive memory leak. Garbage collection doesn't run constantly (as you've observed already, the memory used by all those tables isn't freed up immediately after the OnUpdate run) and constantly creating tables and filling up memory like that is a significant drain on system resources.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
01-20-15, 08:36 AM   #7
CKlausi
A Murloc Raider
 
CKlausi's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2010
Posts: 4
Yes that was my memory problem, now I call the aura update function on events like UNIT_AURA and PLAYER_TARGET_CHANGE etc
OnUpdate only will take care of timers which where stored into duration and expirationTime (also removed the return value, forgot that lua will reference tables, so they will become all the same)
I am also watching the SPELL_AURA_REMOVED event of COMBAT_LOG_EVENT_UNFILTERED to hide auras.
  Reply With Quote
01-20-15, 10:21 AM   #8
jeruku
A Cobalt Mageweaver
 
jeruku's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 223
Thank you for pointing that out, Phanx and Banknorris. I sometimes jumble things together when not actually writing code(well maybe occasionally).

I will edit my previous post to diffuse future confusion.
__________________
"I have not failed, I simply found 10,000 ways that did not work." - Thomas Edison

Last edited by jeruku : 01-20-15 at 10:37 AM. Reason: typo
  Reply With Quote
01-20-15, 01:16 PM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by CKlausi View Post
I am also watching the SPELL_AURA_REMOVED event of COMBAT_LOG_EVENT_UNFILTERED to hide auras.
There is no reason to waste CPU cycles handling CLEU if you're already using UNIT_AURA.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
01-20-15, 04:36 PM   #10
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Originally Posted by Phanx View Post
There is no reason to waste CPU cycles handling CLEU if you're already using UNIT_AURA.
That depends on whether the addon is tracking auras for units that don't currently have a valid unit ID.

Since it's a "dot tracker" I'm guessing they're creating timers for everything they're dotting up.
  Reply With Quote
01-21-15, 07:45 AM   #11
CKlausi
A Murloc Raider
 
CKlausi's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2010
Posts: 4
Thanks for your advice, currently I am only tracking unit ids like target and focus, so I changed it to hide no longer needed bars on UNIT_AURA event.

What would be a good throttle value for onupdate to keep the bars smooth and performance at its best?
  Reply With Quote
01-21-15, 08:41 AM   #12
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Well I would say for a bar addon you shouldn't throttle the updating of the bar value. If you don't, it might end up looking sluggish compared to the rest of the game. It's not all that CPU intensive. I think what most people have an issue with is when you do unnecessary stuff. :P
__________________
Grab your sword and fight the Horde!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Best way to scan unit auras


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