Game Development Community

dev|Pro Game Development Curriculum

Pause singleplayer game on window loose focus and vice versa

by Herru Cules D · 07/11/2008 (6:32 am) · 7 comments

TGE simple script code for singleplayer game mode on window loose focus and vice versa.
orginally from this thread Windowed Mode lose focus...(I'm very thankfull for this thread) and I found this is very usefull in singleplayer game.

You can add this code on any client script, for example in client.cs
function windowFocusChanged(%state)
{
   error("windowFocusChanged" SPC %state);
   if(%state == 0)
   {
      // other gui stuff goin on here.. like push-in pause gui message.

      echo ("PAUSE**");
      $timescale = 0;
   }
   else
   {
      // other gui stuff goin on here.. like pop-in pause gui message.

      echo ("PLAY**");
      $timescale = 1;
   }
}

that's it. hope it helps.

#1
07/11/2008 (12:40 pm)
So how would this work, would i simple bind a key to this command, it will pause the game in a singleplayer enviroment?
#2
07/11/2008 (1:45 pm)
@Edward: It pauses the game when it loses focus and un-pauses when focus is regained. No key binding required.
#3
07/11/2008 (8:12 pm)
@Kevin: thx for the explanation :)

@Edward: u dont have u bind a key for this command. The function is automatically called when the game is not active for example when you uses ALT+TAB to other application window.

hope it helps clear things up :)
#4
07/13/2008 (5:23 pm)
Well in my game I have a clock counting up and wanted to bind it a key, and thanks to danni over at Mirc I got it working..

so for singleplayer with a keybind and also lose focus use this..
in client.cs
$gamePaused = 0;

function windowFocusChanged(%state)
{
   error("windowFocusChanged" SPC %state);
   if(%state == 0)
   {
      // other gui stuff goin on here.. like push-in pause gui message.
      echo ("PAUSE**");
      $timescale = 0;
      Clock.PauseTime(true);
   }
   else
   {
      // other gui stuff goin on here.. like pop-in pause gui message.
      echo ("PLAY**");
      $timescale = 1;
      Clock.PauseTime(false);
   }
}

function pauseGame(%state)
{
   error("windowFocusChanged" SPC %state);
   if(%state == 0)
   {
      if ($gamePaused)
      {
         // other gui stuff goin on here.. like push-in pause gui message.
         echo ("UNPAUSE**");
         $timescale = 1;
         Clock.PauseTime(false);
	     $gamePaused = 0;
      }
      else
      {
         echo ("PAUSE**");
         $timescale = 0;
         Clock.PauseTime(true);
       	 $gamePaused = 1;
      }
   }
}
and for the keybind
in config.cs
moveMap.bind(keyboard, "p", pauseGame);
and default.bind.cs
moveMap.bind(keyboard, p, pauseGame);

TomFeni

thats about it..
#5
08/07/2008 (4:59 am)
Cool stuff... guys
#6
07/28/2009 (11:54 pm)
Note: For TGEA 1.8.1, the callbacks are now "GuiCanvas::onLoseFocus(%this)" and "GuiCanvas::onGainFocus(%this)" in canvas.cs.
#7
04/06/2010 (4:42 pm)
works great - thanks !!!