WoW:Using OnUpdate correctly: Difference between revisions

Using locals instead of globals. See http://www.wowwiki.com/2.0_consolidated_changes (Frame Features)
(demoted headings)
(Using locals instead of globals. See http://www.wowwiki.com/2.0_consolidated_changes (Frame Features))
Line 11: Line 11:
     ...
     ...
     <Scripts>
     <Scripts>
       <OnLoad>this.TimeSinceLastUpdate = 0 </OnLoad>
       <OnLoad>self.TimeSinceLastUpdate = 0 </OnLoad>
       <OnUpdate> MyAddon_OnUpdate(arg1); </OnUpdate>       
       <OnUpdate> MyAddon_OnUpdate(self, elapsed); </OnUpdate>       
     </Scripts>
     </Scripts>
   </Frame>
   </Frame>
  </Ui>
  </Ui>
The important part for us is the "arg1" parameter that we're passing to our function. This is a variable that's being inherited from the WoW system telling us how long it's been since the last update call cycle.
The important part for us is the local "elapsed" (Global is arg1) parameter that we're passing to our function. This is a variable that's being inherited from the WoW system telling us how long it's been since the last update call cycle.


; your_lua_file.lua
; your_lua_file.lua
Line 23: Line 23:
   
   
  -- Functions Section
  -- Functions Section
  function MyAddon_OnUpdate(elapsed)
  function MyAddon_OnUpdate(self, elapsed)
   this.TimeSinceLastUpdate = this.TimeSinceLastUpdate + elapsed;
   self.TimeSinceLastUpdate = self.TimeSinceLastUpdate + elapsed;
   
   
   if (this.TimeSinceLastUpdate > MyAddon_UpdateInterval) then
   if (self.TimeSinceLastUpdate > MyAddon_UpdateInterval) then
     --
     --
     -- Insert your OnUpdate code here
     -- Insert your OnUpdate code here
     --
     --
   
   
     this.TimeSinceLastUpdate = 0;
     self.TimeSinceLastUpdate = 0;
   end
   end
  end
  end
Line 37: Line 37:


If you're doing things that require high accuracy in the frequency of the OnUpdate calls, the above code won't be accurate since every time it is called, you will lose milliseconds as the ''TimeSinceLastUpdate'' variable is reset to 0 (If it was 1.05 seconds since last update, it would take 0.05 more seconds until the next update than it should, assuming that one hits 1.0, otherwise it would be delayed by another 0.05, etc). The below code accounts for this, and has a ''while'' structure to make sure that if the players framerate is very low, it'll still do the updates as many times as is needed.
If you're doing things that require high accuracy in the frequency of the OnUpdate calls, the above code won't be accurate since every time it is called, you will lose milliseconds as the ''TimeSinceLastUpdate'' variable is reset to 0 (If it was 1.05 seconds since last update, it would take 0.05 more seconds until the next update than it should, assuming that one hits 1.0, otherwise it would be delayed by another 0.05, etc). The below code accounts for this, and has a ''while'' structure to make sure that if the players framerate is very low, it'll still do the updates as many times as is needed.
  function MyAddon_OnUpdate(elapsed)
  function MyAddon_OnUpdate(self, elapsed)
   this.TimeSinceLastUpdate = this.TimeSinceLastUpdate + elapsed;
   self.TimeSinceLastUpdate = self.TimeSinceLastUpdate + elapsed;
   
   
   while (this.TimeSinceLastUpdate > MyAddon_UpdateInterval) do
   while (self.TimeSinceLastUpdate > MyAddon_UpdateInterval) do
     --
     --
     -- Insert your OnUpdate code here
     -- Insert your OnUpdate code here
     --
     --
   
   
     this.TimeSinceLastUpdate = this.TimeSinceLastUpdate - MyAddon_UpdateInterval;
     self.TimeSinceLastUpdate = self.TimeSinceLastUpdate - MyAddon_UpdateInterval;
   end
   end
  end
  end


[[Category:HOWTOs|Use OnUpdate Correctly]]
[[Category:HOWTOs|Use OnUpdate Correctly]]
Anonymous user