onUpdate calls vs onRender

Monkey Forums/Monkey Programming/onUpdate calls vs onRender

Xaron(Posted 2013) [#1]
Hi all,

I just was curious how reliable the SetUpdateRate command really is.

Basically you set how often onUpdate is called which should be independent from how long the rendering needs to complete.

This is true up to a point where onUpdate calls get slow down as well. I measured this point and came to a simple formula:

So your update rate can be assured as long as your render loop does not need more than 4 times of the update step size.

An example: If you use a rate of 20 calls ->SetUpdateRate(20) this leads to a step size of 50ms between two onUpdate calls.

Your onRender method may take now up to 4*50=200ms. Till this value your onUpdate still gets its 20 calls per second. If onRender takes longer than 200ms the onUpdate calls will slow down as well.

Using the common update rate of 30, onRender may take up to 4*33ms=132ms and so on...

Strict

Import mojo

Function Main:Int()
  New Test()
  Return 0
End Function

Class Test Extends App
  Field _lastUpdate:Int
  Field _dt:Int
  Field _ticks:Int
  Field _counted:Int

  Method OnCreate:Int()
    SetUpdateRate( 20 )
    _lastUpdate = Millisecs()
    Return 0
  End Method

  Method OnSuspend:Int()
    Return 0
  End Method
  
  Method OnResume:Int()
    Return 0
  End Method

  Method OnUpdate:Int()
    _dt = Millisecs() - _lastUpdate
    If( _dt < 1000 )
      _ticks += 1
    Else
      _lastUpdate = Millisecs()
      _counted = _ticks
      _ticks = 0
    End If
    Return 0
  End Method
  
  Method OnRender:Int()
    Cls
    DrawText( "Counted onUpdate calls per second: " + _counted, 10, 10 )
    Local delayStart:Int = Millisecs()
    Repeat
    Until ( Millisecs()-delayStart ) > 200
    Return 0
  End Method

End Class



muddy_shoes(Posted 2013) [#2]
Your onRender method may take now up to 4*50=200ms. Till this value your onUpdate still gets its 20 calls per second. If onRender takes longer than 200ms the onUpdate calls will slow down as well.


Sort of. The OnUpdate might be called 20 times in a second but it won't be being called every 50ms. So if you only care about the overall rate then it won't be slowed, but if you care about the actual interval it will be intermittently slowed and then repeating way too quickly as it tries to catch up with the requested rate.


Xaron(Posted 2013) [#3]
Hmm yes. That's true. Thanks for the clarification muddy!

So actually my thoughts behind this was to do some better delta timing but the way it works it just don't make any sense to me to do anything in onUpdate instead of onRender...


Gerry Quinn(Posted 2013) [#4]
I thought the magic number was 7 rather than 4 for max updates per render. Maybe Mark changed it.

Yes, I think unless maybe you are doing GUI on a very sluggish system, you probably are better making your own version of OnUpdate() and calling it at the start of OnRender(). That way you'll have alternate updates and renders whatever happens. If you have a physics model that needs a lot of updates, you can always call that multiple times if necessary.