Game Development Community

Pause game and key binding

by Bruno · in Torque Game Builder · 01/14/2006 (7:25 am) · 4 replies

Hi.,

I'm tryng to implement a pause game feature, so that when the player presses "o" it goes back to the game menu while pausing the game, and if the player presses "esc" again while in the menu goes back to the game and unpauses it.
Well, i got it working, except theres a little problem with the way the key binding is working.
When i press "esc" it's like if i was pressing the key, it seems he detects several key hits instead of just one..,

This is my code :

GlobalActionMap.bind(keyboard, "o", main_menu2);


function main_menu2()
{
 if($game_running == 1)
 {
   if($ScenePauseFlag == 0)
   {
     alxStop($game_music);
     Canvas.setContent(mainmenuGui);
     t2dSceneGraph.setScenePause(1);
     $ScenePauseFlag = 20;
     $menu_play = alxPlay( menu_loop );
     echo("pause game and menu");
   }
   else
   if($ScenePauseFlag == 1)
   {
     $ScenePauseFlag = 0;
     alxStop($menu_play);
     $game_music = alxPlay( musicAudio );
     t2dSceneGraph.setScenePause(0);
     Canvas.setContent(mainScreenGui);
     echo("unpause game and lets play");
   }
 }
}

#1
01/14/2006 (7:42 am)
If you define the function like

function main_menu2(%val)
{
...
}
%val is 1 when the function is called when the key is pressed.
%val is 0 when the function is called when the key is released.

Was that what you were looking for or am i misunderstanding?
#2
01/14/2006 (7:50 am)
You misunderstood :)

What i mean is , the variable $ScenePauseFlag is initialized at zero.
When i press it, the game is indeed pause, menu music is played, gameplay music is stoped and i let $ScenePauseFlag = 1.
In the next time when i press the key, $ScenePauseFlag = 1, menu music stops, gameplay music kicks in and i show the game canvas.
The problem is that when i press esc, its like if T2D ignores the ifs, he runs what is inside the first IF and then what is inside the 2nd If.., its like if he runs it like this :

alxStop($game_music);
Canvas.setContent(mainmenuGui);
t2dSceneGraph.setScenePause(1);
$ScenePauseFlag = 1;
$menu_play = alxPlay( menu_loop );
echo("pause game and menu");
$ScenePauseFlag = 0;
alxStop($menu_play);
$game_music = alxPlay( musicAudio );
t2dSceneGraph.setScenePause(0);
Canvas.setContent(mainScreenGui);
echo("unpause game and lets play");

I say this because i never see the game menu, and the output of the console when i press the key is this :
echo("pause game and menu");
echo("unpause game and lets play");

If in the first IF clause, i assign $ScenePauseFlag = 20; so that he never gets into the 2nd IF, i can see the menu just fine.., so my guess is that T2D is reading multiple key presses when i just press it one single time...
#3
01/14/2006 (8:09 am)
I should have explained what i meant a bit clearer.

When you press that "o" key and call main_menu2, it will run that function twice. once when you press and once when you release.

if you define the function like
function main_menu2(%val)
{
..
}
that value passed in %val is 1 when the key is pressed and 0 when it is released.

With that you can check if the function was called with a key press, and if so run the code.
It should work if you put the following around your existing code inside main_menu2()
if (%val)
{
...
}

Does that make it a bit clearer? and if i'm still misunderstanding well, i dont know :P i'll take another read.
#4
01/14/2006 (9:32 am)
I got it, thanks..
I wasn't aware that the same function would run twice for each keypress.