- Home /
How to pause a game?
Here's the problem: I'd like to pause the game but still have my menus work. If I set time.scale = 0.0, it pauses everything in the game, including my animated menus (which are game objects).
Ideally what I'd like to do is set the time scale for groups of objects or layers in the scene, but as far as I know that's not possible.
The only other choice I can think of is to do it by brute force through scripting, which is an ugly prospect. My game has a lot of complex behaviors that trigger each other with timed delays. I'd have to intercept all of those delays and rebuild them from the point they were paused, which opens the door to a lot of potential bugs.
Is there some other approach I'm overlooking?
In a previous project I had a gameobject with a single script to handle game pausing. Others would read his state in order to do their logic or not. But as you stated that you have timers and stuff that might get too messy since you would need to track the pause time and discount it from your other time calculations.
Answer by Steven-Walker · Dec 07, 2011 at 11:51 PM
The solution I've chosen is to pause the playback of key elements via script and to leave the time scale untouched. This involved creating a handler for time triggers that keeps track of every trigger. When the game is paused, all active triggers get stopped and their current times recorded. When the user resumes the game, all of the triggers are recreated from the time it was paused. Any behaviors/animations that started before the pause continue to play, but new ones won't trigger. So in effect, the pause is a 'living hold', which in some ways is better than a 100% pause.
Thanks WillTAtl for your help.
Answer by WillTAtl · Dec 07, 2011 at 08:38 PM
assuming they don't use physics, you can make your gui objects immune by just calculating the real time elapsed using Time.realtimeSinceStartup; there's no equivilant realtimeDelta, but you could calculate your own by storing the realtime of the previous update in a variable. Something like this...
private var prevTime:float=0;
function Update()
{
var timeNow=Time.realtimeSinceStartup;
var deltaRealtime=timeNow-prevTime;
prevTime=timeNow;
//then write your existing code, using deltaRealtime instead of
//Time.deltaTime or timeNow instead of Time.time for any lerps or other
//time-based calculations
}
This could potentially cause issues if your framerate drops significantly, and realtime gets way out of synch with game time, but depending on what you're doing with the gui objects it may not be a real problem.
What about animations and using WaitForSeconds? $$anonymous$$y menus/navigation have animation tracks and I use yield to time the opening and closing of items.
you could fairly easily implement a WaitForRealSeconds() function, like this:
function WaitForRealSeconds(seconds:float) : IEnumerator
{
var waitUntil=Time.realtimeSinceStartup+seconds;
while (waitUntil>Time.realtimeSinceStartup)
yield;
}
then in your time-immune functions, just yield to that ins$$anonymous$$d of WaitForSeconds(). This is even more prone to giving wonky behavior if your framerate drops very low, but if it's just menu open/close effects, the worst case should be that with framerate low enough the player can't see the animation, and it seems to happen instantly.
I've decided that getting all my UI behaviors to work without time is too difficult. I'm not just using yields, but also iTween and animations, which pose much more complex problems.