12.07.2015 Views

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

356 Part III ■ Advanced Addon Techniques-- Don’t warn the user at the same level more than onceelseif percFree == 0 thenmsg = frame.fullMessageelseif percFree = self.THROTTLE thenbagWatch_ScanBags(self)counter = 0self:Hide()endend)BagWatchFrame:SetScript(“OnEvent“, function(self, event, ...)BagWatchFrame:Show()end)Rather than calling the bag scan function (which has been renamed) directly,the OnEvent h<strong>and</strong>ler just shows BagWatchFrame,causingtheOnUpdate script t<strong>of</strong>ire. Once the configured amount <strong>of</strong> time has passed (in this case 0.5 seconds),the bag scan will occur. This means that even if several BAG_UPDATE events firewithin that half-second period, the scan will only be run once.Repeating Code with OnUpdateYou can also use OnUpdate scripts to run code repeatedly in order to per<strong>for</strong>msome periodic calculation. For example, you could alter the CombatTrackeraddon you created in Chapter 14 to update the frame every second or soduring combat, giving you a running average instead <strong>of</strong> only displaying atthe end.Add the following function to the end <strong>of</strong> CombatTracker.lua:local throttle = 1.0local counter = 0function CombatTracker_OnUpdate(self, elapsed)counter = counter + elapsedif counter >= throttle then

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!