Thread Tools Display Modes
08-21-14, 07:36 PM   #1
darrare
A Defias Bandit
Join Date: Aug 2014
Posts: 3
Help with an addon i am making.

Im making an addon that tracks combat stats for a hunter such as agility, ap, crit, mastery, haste, etc.

i have it displaying correctly, but my only problem is i have no way for it to update at a reasonable rate.

local function updateFunction()
AgilityLine.text:SetText("Agility = ".. getRangedAgility())
AttackPowerLine.text:SetText("AP = ".. getRangedAttackPower())
CritLine.text:SetText("Crit = ".. getRangedCrit() .."%")
MasteryLine.text:SetText("Mastery = ".. getRangedMastery())
HasteLine.text:SetText("Haste = ".. getHaste() .."%")
end

i have this function that updates it correctly, but i have no idea how to impliment a way to make it only update when it should, and not 60 times a second.

any help would be appreciated
  Reply With Quote
08-21-14, 07:40 PM   #2
Yafis
A Fallenroot Satyr
 
Yafis's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2014
Posts: 29
You have update on OnUpdate?
  Reply With Quote
08-21-14, 08:01 PM   #3
darrare
A Defias Bandit
Join Date: Aug 2014
Posts: 3
Originally Posted by Yafis View Post
You have update on OnUpdate?
not sure i understand, is OnUpdate some sort of built in function?
  Reply With Quote
08-21-14, 08:29 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
For now, you need to create a frame that'll run the code in its OnUpdate handler.
lua Code:
  1. local lastupdate=0;--   Local storing how long since our last update
  2. local frame=CreateFrame("Frame");-- Our frame
  3.  
  4. frame:SetScript("OnUpdate",function(self,elapsed)
  5.     lastupdate=lastupdate+elapsed;--    Add elapsed to update local
  6.     if lastupdate>1 then--  If it's been a second, update
  7.         AgilityLine.text:SetText("Agility = ".. getRangedAgility());
  8.         AttackPowerLine.text:SetText("AP = ".. getRangedAttackPower());
  9.         CritLine.text:SetText("Crit = ".. getRangedCrit() .."%");
  10.         MasteryLine.text:SetText("Mastery = ".. getRangedMastery());
  11.         HasteLine.text:SetText("Haste = ".. getHaste() .."%");
  12.  
  13.         lastupdate=0;-- Reset to zero
  14.     end
  15. end);



In WoD, we'll be getting a new C_Timer system to handle this.
When this happens, your code would look like this.
lua Code:
  1. C_Timer.NewTicker(1,function()--    Register a function to update every second
  2.     AgilityLine.text:SetText("Agility = ".. getRangedAgility());
  3.     AttackPowerLine.text:SetText("AP = ".. getRangedAttackPower());
  4.     CritLine.text:SetText("Crit = ".. getRangedCrit() .."%");
  5.     MasteryLine.text:SetText("Mastery = ".. getRangedMastery());
  6.     HasteLine.text:SetText("Haste = ".. getHaste() .."%");
  7. end);



Note: To enable Lua syntax highlighting on these forums, surround the code with [highlight=lua] [/highlight].
This is also visible as a Lua button on the right side of the formatting toolbar in the message editor.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 08-21-14 at 08:39 PM.
  Reply With Quote
08-21-14, 09:03 PM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
I'm pretty sure there are events that fire when these stats changed, so there's no reason to use an OnUpdate script here. Just register for the event(s) that tell you when your stats change, and update when they fire:

Code:
local f = CreateFrame("Frame")
f:RegisterEvent("HEY_YOUR_STATS_CHANGED") -- not the real event name
f:RegisterEvent("SOME_OTHER_EVENT")
f:SetScript("OnEvent", function(self, event, ...)
     -- update the display here
end)
Use the /eventtrace command in-game, do something that changes your stats, and look in the window to see which event fired. I recommend either doing this far away from other players, or leaving /eventtrace open for a minute beforehand and removing all the irrelevant events that are filling the window (hover and click the X).
__________________
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
08-22-14, 02:24 AM   #6
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
The event you looking for is "UNIT_STATS".
  Reply With Quote
08-23-14, 03:25 PM   #7
Cybeloras
A Fallenroot Satyr
 
Cybeloras's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 28
You will also need COMBAT_RATING_UPDATE since UNIT_STATS will only cover changes in agility.
  Reply With Quote
08-24-14, 10:14 AM   #8
MaLarsson
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Aug 2014
Posts: 13
I feel that it is a good practice to always check which events you want to listen to before beginning with the code.
Wowwiki has a list of all the events: http://www.wowwiki.com/Events_A-Z_(Full_List)
You can try adding something like this to your code:
Lua Code:
  1. local frame, events = CreateFrame("Frame", nil, UIParent), {}
  2.  
  3. function events:UNIT_STATS(unitID)
  4.    if unitID == "player" then
  5.       updateFunction()
  6.    end
  7. end
  8.  
  9. function events:COMBAT_RATING_UPDATE()
  10.    updateFunction()
  11. end
  12.  
  13. for k, v in pairs(events) do
  14.    frame:RegisterEvent(k)
  15. end
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Help with an addon i am making.


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