View Single Post
02-28-13, 11:31 AM   #5
ravagernl
Proceritate Corporis
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,176
Originally Posted by Mayron View Post
This is sort of worrying because I am using many OnUpdate KgPanel scripts such as this:
[...]

I know this might be off topic but should I be worrying about this? If so what should I be doing instead?
Like said, frames run their OnUpdate script on every frame per second as long as the frame is shown.
One thing you can use if you absolutely must use Onupdate is what we like to call a "bucket timer". You use a variable that grows every frame, if the variable is more then the number of seconds between each bucket, you reset the variable and run a script.
For example: You want to run a script each second (for a simple clock panel)
OnLoad:
lua Code:
  1. self.elapsed = 0
OnUpdate:
lua Code:
  1. self.elapsed = self.elapsed + elapsed -- increment by time passed since last frame update
  2. if self.elapsed < 1 then return end -- has a second elapsed
  3. self.elapsed = 0 -- reset the timer
  4. self.text:SetFormattedText("%02d:%02d", GetGameTime())

Another or better thing you could do, is register for the correct event that you need, for example, PLAYER_LOGIN, which only triggers when you login:
OnLoad:
lua Code:
  1. self:RegisterEvent('PLAYER_LOGIN')
OnEvent
lua Code:
  1. -- Conditional checking on events is totally optional. I usually omit it when I only need to listen for one event.
  2. if event == 'PLAYER_LOGIN' then
  3.     -- do stuff
  4. end

Your example script could be converted to use events (ADDON_LOADED):
lua Code:
  1. if not IsAddOnLoaded("Chatter") then
  2.     self:RegisterEvent('ADDON_LOADED')
  3.     self:Hide()
  4. else
  5.     self:ConditionalShow()
  6. end
  7.  
  8. -- define a function so we dont have to paste the same code in the onevent box.
  9. function self:ConditionalShow()
  10.     if LibStub("AceAddon-3.0"):GetAddon("Chatter").db:GetCurrentProfile() == "MayronUI" then
  11.         self:Show()
  12.     else
  13.         kgPanels:FetchFrame("chatB"):Show()
  14.         Bazooka.db:SetProfile("MayronUI2")
  15.         self:Hide()
  16.     end
  17.     -- Chatter is loaded here so we dont have to listen to ADDON_LOADED anymore, clean up this frame
  18.     self:UnregisterEvent('ADDON_LOADED')
  19.     self:SetScript('OnEvent', nil)
  20.     self.ConditionalShow = nil
  21. end

OnEvent:
lua Code:
  1. if event == 'ADDON_LOADED' and arg1 == 'Chatter' then
  2.     self:ConditionalShow() 
  3. end
My example triggers when Chatter is being loaded as well, without running every frame.

EDIT: I realised you can check if a user has an AddOn enabled or if it is load on demand: GetAddOnInfo

New logic should then be:
Code:
if user has AddOn enabled
    If addon is not yet loaded, 
        register for ADDON_LOADED
        set onevent handler for ADDON_LOADED here
    else
         run specific code for that addon
    end
end
Happy coding

Last edited by ravagernl : 02-28-13 at 12:11 PM.
  Reply With Quote