Game Development Community

Pause the Game

by Demolishun · in Torque Game Engine · 12/11/2005 (8:24 pm) · 19 replies

How do you pause the game? I mean all simualtion, sound, and everything stops. The user interface works (not a lockup), but the game itself pauses.

About the author

I love programming, I love programming things that go click, whirr, boom. For organized T3D Links visit: http://demolishun.com/?page_id=67


#1
12/11/2005 (8:37 pm)
I think there is a game time if you could find a way to stop it entirely...
#2
12/11/2005 (8:48 pm)
Td2 has a total pause. I don't know about TGE didn't see much in search.
#3
12/11/2005 (9:20 pm)
Okay, I traced into game/main.cc and found two calls to advancing time that look promising:
Platform::advanceTime(elapsedTime);
Sim::advanceTime(timeDelta);

This look like they may be what I am looking for. I will play with these and see what I come up with. Obviously there may be networking issues and the like. Like heart beats should be kept alive, but at least it could be used for single player when not playing a networked mission game. You know, like when you are playing the computer.
#4
12/11/2005 (9:41 pm)
Okay, found a really interesting toy that could be my answer:
timeScale

Set this to zero and everything stops. There is some event processing issues, but that could just be a flush to the system to the events to fix that.

Set this to 20 and the clouds move way to fast! This is really cool thing to play with!

Usage:
$timeScale = 0; // stop time
$timeScale = 1; // normal time
$timeScale = 20; // your world just got a lot faster
$timeScale = 0.1 // slowwww dowwwwn

There is also:
timeAdvance

This works for slowing down, but zero causes normal time flow. WIll not stop time.

I do not know if these affect networking, they do not slow down wave files.
#5
12/12/2005 (5:40 am)
Dude, $timeAdvance rules! Why didn't I heard of it before?

I tested it, and what it does is specify how much time the simulation advances per frame, in milliseconds. When it's set to 0, the engine works in automatic mode, adjusting the time per frame based on the framerate (frameskipping). But when set to anything else, the engine will update a fixed amount of time every frame, making the simulation run faster or slower depending on the framerate.

This is the holy graal to make nice video captures, or when making certain kinds of game where you want a fixed per-frame time advance (old-school 2D shooters comes to mind).
#6
12/12/2005 (6:14 am)
For single player games a function like this in default.bind.cs seems to do the trick:

function pauseGame()
{
$timescale = 0;
alxStopAll();
}
moveMap.bind( keyboard, q, pauseGame );

function UnpauseGame()
{
$timescale = 1;
}
moveMap.bind( keyboard, x, UnpauseGame );

If you wanna have it so the same key is responsible for pausing and unpausing (i.e. Press 'P' to pause and 'P' to unpause) here's a quick hack that seems to work well...

In default.bind.cs place this somewhere...

function PopActionMap()
{
   echo("Popping Player action map " );
   movemap.pop();
}

function PushActionMap()
{
   echo("Pushing Player action map " );
   movemap.push();
}

function PopActionMapPause()
{
   echo("Popping Pause action map " );
   pauseMap.pop();
}

function PushActionMapPause()
{
   echo("Pushing Pause action map " );
   pauseMap.push();
}

function pauseGame()
{
   PopActionMap();
   PushActionMapPause();
   $timescale = 0;
   alxStopAll();
}

moveMap.bind( keyboard, p, pauseGame );

Then create a new script called 'pauseMap.cs' and place it in your client/scripts directory. Make sure you execute it within client/init.cs (e.g. exec("./scripts/pauseMap.cs");. Inside this new script place the following:

if ( isObject( pauseMap ) )
   pauseMap.delete();
new ActionMap(pauseMap); 
//-----------------------------------------------------------------------------

function UnpauseGame()
{
   PopActionMapPause();
   PushActionMap();
   $timescale = 1;
}

pauseMap.bind( keyboard, q, UnpauseGame );

That's it, now when you press 'P' the game will pause, all sounds will stop, and when you press 'P' again the game resumes.

P.S. Make sure 'P' isn't assigned to anything else in default.bind.cs
#7
12/12/2005 (8:40 pm)
Also, you may need to create a Pause GUI that takes focus and will grab inputs so when you unpause it does not apply a whole bunch of mouse/keyboard moves.

Manoel,
That is a really cool idea! I thought of another where you put a bunch of AI in the game and regression test at high speed to get metrcis on the bot performance. Another could be battle simualtions that occur in memory only to simulate battles that occur, but are not seen. This would help avoid random number generated outcomes for battles in lets say a persistant universe.
#8
12/12/2005 (9:01 pm)
Quote:
Also, you may need to create a Pause GUI that takes focus and will grab inputs so when you unpause it does not apply a whole bunch of mouse/keyboard moves.

This won't happen cos when you pause the 'player action map' is being popped and a new 'pause action map' is being pushed with only the unpause key bound within it. When you unpause the opposite happens. So whilst in paused mode you can press all the keys you want and they won't ever register, with the exception of the one key bound to unpause.
#9
03/26/2013 (7:53 pm)
t3d do not have alxStopAll();

what can be use instead of it?

#11
03/27/2013 (11:39 am)
SFXStopAll();
#12
03/27/2013 (1:26 pm)
not working.
there is no function named "SFXStopAll();"



does "$timescale = 0;" also disable sound?
#13
03/27/2013 (6:14 pm)
should not let me go hit pause. I'm using SFXStopAll in code ill check it now.
#14
03/27/2013 (6:28 pm)
Okay to stop all sounds, you need to specify what channel you want to stop:

sfxStopAll($GuiAudioType);
      sfxStopAll($SimAudioType);
      sfxStopAll($MessageAudioType);
      sfxStopAll($MusicAudioType);
      sfxStopAll(0);

But this will stop all sounds to where you can't continue them.

If you are pausing the game and want to unpause and continue you might want the code i am using to pause a cutscene.



$CS::MUS_ID=0;

   function CSSIM_PlayMusic(%isLooping,%MusicPath){
      sfxStop($CS::MUS_ID);
      if(%isLooping){
         $CS::MUS_ID=sfxPlayOnce( AudioMusicLoop2D, %MusicPath );  
      }else{
         $CS::MUS_ID=sfxPlayOnce( AudioMusic2D, %MusicPath );  
      }
   }
   function CSSIM_StopMusic(){
      sfxStop($CS::MUS_ID);
   }
   
   function pauseCutscene(){
      $CS::PAUSED=1;
      $timescale = 0;  
      SkipMessageArea.visible=true;
      CSChoice.setText("<YES>");
      $CS::PAUSE_ACTION=1;
      
      if(isObject($CS::VOCAL_ID))$CS::VOCAL_ID.Pause();
      if(isObject($CS::MUS_ID))$CS::MUS_ID.Pause();
   }


function selectCutsceneChoice(%action){
   switch(%action){
      case 0:
         switch($CS::PAUSE_ACTION){
            case 0: //Continue CS
               $CS::PAUSED = 0;
               $timescale  = 1;  
               $CS::ENDED = 0;
               SkipMessageArea.visible=false;
               if(isObject($CS::VOCAL_ID))$CS::VOCAL_ID.Play();
               if(isObject($CS::MUS_ID))$CS::MUS_ID.Play();
            case 1: //Skip CS
               $CS::PAUSED = 0;
               $CS::ENDED = 1;
               $timescale  = 1;  
               SkipMessageArea.visible=false;
         }
      case  1 :
         if(CSChoice.text $= "<YES>"){
            CSChoice.setText("<NO>");
            $CS::PAUSE_ACTION=0;
         }else{
            CSChoice.setText("<YES>");
            $CS::PAUSE_ACTION=1;
         }
      case -1 :
         if(CSChoice.text $= "<YES>"){
            CSChoice.setText("<NO>");
            $CS::PAUSE_ACTION=0;
         }else{
            CSChoice.setText("<YES>");
            $CS::PAUSE_ACTION=1;
         }
   }
}
#15
03/27/2013 (7:14 pm)
problem is i am not finding defination of "sfxStopAll" anywhere.

"But this will stop all sounds to where you can't continue them. "
yes.i tried that and obviously there is no way to resume them.
#16
03/27/2013 (7:19 pm)

you can resume with the method above.

sfxStopAll is in core for T3D.

function sfxStopAll( %channel )
{
   // Don't stop channel itself since that isn't quite what the function
   // here intends.
   
   %channel = sfxOldChannelToGroup( %channel );
   if (isObject(%channel))
   {
      foreach( %source in %channel )
         %source.stop();
   }
}



#17
03/27/2013 (9:16 pm)
what an idiot!!!!!!!
i should have searched in script.

anyway,thanks for pointing out to sfxStopAll.
that should stop all sound.

but not sure about resuming.i will do a test .
#18
03/28/2013 (3:00 am)
If you need to resume the sound you will need to do this:

//Create a holder for the SFXSource that is returned when a sound play is executed
$CS::MUS_ID=0;

//When you play the sound store the SFXSource that is passed back from the play function.
   function CSSIM_PlayMusic(%isLooping,%MusicPath){
      sfxStop($CS::MUS_ID);
      if(%isLooping){
         $CS::MUS_ID=sfxPlayOnce( AudioMusicLoop2D, %MusicPath );  
      }else{
         $CS::MUS_ID=sfxPlayOnce( AudioMusic2D, %MusicPath );  
      }
   }

//To Stop the Music Channel 
   function CSSIM_StopMusic(){
      sfxStop($CS::MUS_ID);
   }
   
//To Pause the Music Channel 
   function CSSIM_PauseMusic(){
      if(isObject($CS::MUS_ID)) $CS::MUS_ID.Pause();
   }
   
//To Resume the Music Channel 
   function CSSIM_ResumeMusic(){
      if(isObject($CS::MUS_ID))$CS::MUS_ID.Play();
   }
#19
03/30/2013 (7:11 am)
Quote:
sfxStopAll is in core for T3D.

Yeah, but this is the TGE forum....

In TGE, alxStopAll() should work - it's in audio/audioFunctions.cc (just like T2D). However, there is no way to pause sound - the changes made to the audio files in T2D should work here though.