Originally Posted by Mayron
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:
OnUpdate:
lua Code:
self.elapsed = self.elapsed + elapsed -- increment by time passed since last frame update
if self.elapsed < 1 then return end -- has a second elapsed
self.elapsed = 0 -- reset the timer
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:
self:RegisterEvent('PLAYER_LOGIN')
OnEvent
lua Code:
-- Conditional checking on events is totally optional. I usually omit it when I only need to listen for one event.
if event == 'PLAYER_LOGIN' then
-- do stuff
end
Your example script could be converted to use events (ADDON_LOADED):
lua Code:
if not IsAddOnLoaded("Chatter") then
self:RegisterEvent('ADDON_LOADED')
self:Hide()
else
self:ConditionalShow()
end
-- define a function so we dont have to paste the same code in the onevent box.
function self:ConditionalShow()
if LibStub("AceAddon-3.0"):GetAddon("Chatter").db:GetCurrentProfile() == "MayronUI" then
self:Show()
else
kgPanels:FetchFrame("chatB"):Show()
Bazooka.db:SetProfile("MayronUI2")
self:Hide()
end
-- Chatter is loaded here so we dont have to listen to ADDON_LOADED anymore, clean up this frame
self:UnregisterEvent('ADDON_LOADED')
self:SetScript('OnEvent', nil)
self.ConditionalShow = nil
end
OnEvent:
lua Code:
if event == 'ADDON_LOADED' and arg1 == 'Chatter' then
self:ConditionalShow()
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