Pausing the Game: strategies

Monkey Forums/Monkey Programming/Pausing the Game: strategies

AdamRedwoods(Posted 2011) [#1]
Ok, I've run into a little problem and have a potential solution, but was wondering if anyone else thought of different solutions.

In my game I use Millisecs() for timing a lot of activity: firing rates of tanks, enemy spawning, build times, etc.

Pausing the game, the system clock keeps going. This is probably the same for every target (mobile particularly, when you answer a call). I'm just curious as to what other people have devised as a solution. My solution is just to store a clock-differential in each unit class, enemy class, etc. and reset the clocktick I'm using for each particular scenario.

I'm now also wondering if I need a clock-class to handle things in the future.


Perhaps I'm just typing aloud to myself.


Gerry Quinn(Posted 2011) [#2]
You need a game clock, IMO. Something like this:

Method OnUpdate:Int()
	
	Local millisNow:Int = Millisecs()
	Local millisPassed:Int = millisNow - lastMillis
	lastMillis = millisNow
	millisPassed = Min( millisPassed, 100 )
	gameClock += millisPassed
	
	game.Update( millisPassed )
		
	Return 0
end


Some objects will just use millisPassed directly, others can access the gameClock if they need more advanced knowledge of the in-game time.

The line with Min() is designed to slow the game a bit if the framerate drops very low for some reason. Of course whether you do that is up to you.


AdamRedwoods(Posted 2011) [#3]
Thank you for that, seems like a simpler solution than what I had:

Class GameTick
	Global ms:Int =0
	Field nextTick:Int
	Field rate:Int
	
	Method Set(t:Int)
		If Not ms Then ms = Millisecs()
		rate = t
		nextTick = ms + t
	End
	Method Stop()
		nextTick = 0
	End
	Method Check:Bool()
		If ms > nextTick And nextTick
			Local dif:Int = Min(ms-nextTick , 500) ''difference, in case we are falling behind
			nextTick = ms+rate-dif
			Return True
		Else
			Return False
		Endif
	End
	
	Function OnUpdate()
		ms = Millisecs()
	End
End



GfK(Posted 2011) [#4]
What i do, is have a startTime var which i log Millisecs() into. Any in-game timers i use are based on this.

In OnSuspend(), i store the time at which the game was paused. In OnResume() I calculate the length of time the game was paused for (currentTime - pausedTime). Then simply add that value onto startTime.


AdamRedwoods(Posted 2011) [#5]
Right GFK, that's what I was also thinking.

But additionally, the millisecsPassed routine with Min() may be a nice solution.

The problem I had was in the pausing or AppSuspend() state.
Millisecs() keeps going, so if the base clock with Millisecs() needs either a differential for when the game is paused, or in miilisecsPassed, it will only increment the gameClock by a maximum of 100ms.