WoW:Using OnUpdate correctly: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
No edit summary
 
mNo edit summary
Line 1: Line 1:
first of all you have to have a main frame where you define the onUpdate function that will be called on every update. it is important, that this frame has the tags stated below ... hidden="false" toplevel="true", otherwise your function will not be called
= How often is it called =
The game engine will call your OnUpdate function approximately every 0.1 seconds (as defined by the UPDATE_DELAY [[WoW Constants]]). This is usually more often then necessary, and executing your code so often will cause lag!  What you need to do is limit how often your OnUpdate code runs, strategies are outlined bellow.


-------------------------
= When is it called =
this is in your XML file.
OnUpdate is not called on any hidden frames, only while they are shown on-screen.  OnUpdate will also never be called on a virtual frame, but will be called on any frames that inherit from one.
-------------------------


= Proper Use Example =
; your_xml_file.xml
  <Ui ...>
   <Frame name="MyAddon_MainFrame" parent="UIParent" hidden="false" toplevel="true">
   <Frame name="MyAddon_MainFrame" parent="UIParent" hidden="false" toplevel="true">
     <Frames>
     ...
    </Frames>
     <Scripts>
     <Scripts>
       <OnUpdate>
       <OnUpdate> MyAddon_OnUpdate(elapsed); </OnUpdate>       
        MyAddon_OnUpdate(arg1);
      </OnUpdate>       
     </Scripts>
     </Scripts>
   </Frame>
   </Frame>
  </Ui>


-------------------------
; your_lua_file.lua
this is in your LUA file
-------------------------


   MyAddon_LastUpdate = 0;
   -- Globals Section
   MyAddon_Update_Interval = 1; --the update interval in seconds
   MyAddon_updateInterval = 1; -- how often my OnUpdate code will run


  -- Functions Section
   function MyAddon_OnUpdate(elapsed)
   function MyAddon_OnUpdate(elapsed)
     MyAddon_LastUpdate = MyAddon_LastUpdate + elapsed;
     this.timeSinceLastUpdate = this.timeSinceLastUpdate + elapsed;
     if (MyAddon_LastUpdate > MyAddon_Update_Interval) then     
     if (this.timeSinceLastUpdate > MyAddon_updateInterval) then     
       MyAddon_LastUpdate = 0;
       --
       --do Stuff here
       -- Insert your OnUpdate code here
      --
      this.timeSinceLastUpdate = 0;
     end
     end
   end
   end


for '--do stuff here' you should insert your own sourcecode that should be run every 'MyAddon_Update_Interval' seconds. of course you should replace all function / variable names ( MyAddon_ ) with a unique string representing your addon.
Insert your code at the '-- Insert your OnUpdate code here', and substitute the 'MyAddon_' with your addon's name.  You might also want to tweak your 'MyAddon_updateInterval' global variable to reflect how often your addon needs the code run, the less the better.  This solution also happens to be thread-safe, which is another issue when your code is being run every 0.1 seconds, finished or not.
 
[[Category:HOWTOs]]

Revision as of 23:33, 30 August 2005

How often is it called

The game engine will call your OnUpdate function approximately every 0.1 seconds (as defined by the UPDATE_DELAY WoW Constants). This is usually more often then necessary, and executing your code so often will cause lag! What you need to do is limit how often your OnUpdate code runs, strategies are outlined bellow.

When is it called

OnUpdate is not called on any hidden frames, only while they are shown on-screen. OnUpdate will also never be called on a virtual frame, but will be called on any frames that inherit from one.

Proper Use Example

your_xml_file.xml
 <Ui ...>
 <Frame name="MyAddon_MainFrame" parent="UIParent" hidden="false" toplevel="true">
   ...
   <Scripts>
     <OnUpdate> MyAddon_OnUpdate(elapsed); </OnUpdate>      
   </Scripts>			
 </Frame>
 </Ui>
your_lua_file.lua
 -- Globals Section
 MyAddon_updateInterval = 1; -- how often my OnUpdate code will run
 -- Functions Section
 function MyAddon_OnUpdate(elapsed)
   this.timeSinceLastUpdate = this.timeSinceLastUpdate + elapsed; 	
   if (this.timeSinceLastUpdate > MyAddon_updateInterval) then    
     --
     -- Insert your OnUpdate code here
     --
     this.timeSinceLastUpdate = 0;
   end
 end

Insert your code at the '-- Insert your OnUpdate code here', and substitute the 'MyAddon_' with your addon's name. You might also want to tweak your 'MyAddon_updateInterval' global variable to reflect how often your addon needs the code run, the less the better. This solution also happens to be thread-safe, which is another issue when your code is being run every 0.1 seconds, finished or not.