Delta timing and OnSuspend/OnResume issues

Monkey Forums/Monkey Programming/Delta timing and OnSuspend/OnResume issues

Sensei(Posted 2014) [#1]
Hey guys,

I wonder if any of you have come across this little problem.
I use the Delta timer code from the delta.monkey example and I've just recently noticed that when you have
#MOJO_AUTO_SUSPEND_ENABLED = True


In your game and the OnSuspend is called, that it seems to me List objects and other things that use the delta timing continue to update!
VIsually the game suspends, but when it gains focus again, the game timer and other things I have used that use the timer are clearly updated as if the delta timer is trying to make up for the gap of time between OnSuspend and OnResume.

I found the only way to "fix" this is to stop using delta timing, but I don't see this as an option.
Normally I wouldn't bother with OnSuspend and OnResume, however since I want to release my game on both browser and Android versions, using delta timing is rather important.

As a simple example, I will have this snippet
Method OnUpdate:Int()
  dt.UpdateDelta
  ...


Then later on in the same OnUpdate method, I have
attacktimer += 1 * dt.delta ' attacktimer is a float, btw


When you lose focus, you can visually see the timer (I display it in OnRender) and you would for example lose focus at say 100, then wait a few seconds and return to it and the timer would be on say 200, when it should resume at 101. I've noticed this also happens with List objects that use delta timing. For example, I have a list of enemy ships and in the OnUpdate method I call the Enemy.Update() method and pass the dt.delta value through as the ships movements and bullets all also use delta timing to ensure movement speed remains constant regardless of FPS, for example
For Local i := Eachin enemyList
	i.x += i.hSpeed * timeDilationSpeed * delta
....
Next


When I comment out * delta, it suspends correctly, otherwise it just continues to update so that when the game starts rendering again, the ships are already out of the screen or just about (depending on how long you left focus).

I have tested this on browser, and GLFW and it happens each time.

From my experience it seems that in the OnSuspend method, you can only call a method once and it runs through it literally only once, so you can't do much during that time to call other methods to do anything of use, like passing a variable to the enemy update method to say don't update if this variable is 1 or something.

I admit I'm not very experience with this but at the end of the day, I would like to implement my own pause method which is fine, but it will be useless on browser and android/ios systems unless I can somehow use the OnSuspend method with my own pause.

Any help would be appreciated.

Thanks,
Jaco

[edit]
In fact, I just noticed the exact same thing happens on delta.monkey example. Try it for yourself in the browser.


therevills(Posted 2014) [#2]
Within the OnResume method you need to reset the delta:

Method OnResume:Int()
	dt.currentticks = Millisecs()
	dt.lastticks = dt.currentticks
End



MikeHart(Posted 2014) [#3]
When you use OnSuspend and OnResume you need to run your own timer. Basicslly what you have described by adding the time difference of each frame to it. The great advantage is that you can multiply this by a defined factor and so speed up your game or slow it down.


Sensei(Posted 2014) [#4]
Oh thanks therevills! I think that is exactly what I need. Wish I'd thought of that!
I'll give it a try tonight..


Gerry Quinn(Posted 2014) [#5]
My usual technique is to read the time on every update, but lock it to a certain range of possibilities, so for example:

Local millis:Int = Millisecs() - lastMillis
lastMillis = Millisecs()
Local timePassed:Float = Max( 0.01, Min( millis * 0.001, 0.10 ) )
gameTime += timePassed

This way lags or interruptions get accommodated, but can't really mess up anything.


Sensei(Posted 2014) [#6]
Thanks so much guys. These are really useful tips. Would've been nice if they were documented but at least now there are forum threads for it :)


Sensei(Posted 2014) [#7]
Well @therevills, your code did the trick. Many thanks!
@Gerry, I like your idea and I think I'm going to give it a try in my next game as I know so much more now than I did at the start of this game project :)