Inside the game loop, the game is paused by pressing P, meaning that the game loop does not run anymore. Problem is that after this loop is halted, P cannot be pressed again to resume the loop, since it was inside the loop itself. How to make it so that when P is pressed again it makes the loop work again?
Asked
Active
Viewed 2,804 times
2 Answers
21
Don't actually stop the game loop when pausing the game. Instead, you have to add bool variable, that is changed to true/false depending if game is paused. If game is paused, you only have to stop updating the game, but you can still keep rendering the current frame including getting updates from input.
if(gameIsPaused == false)
{
// run updates
}
checkInput();
render();
1
Make it so that pressing P switches to another loop that handles the game state "Paused" and allows to transition back to the original game loop by pressing P again.
You can actually put this all into the game loop itself and introduce a state variable, if you want and instead of turning off the loop, the state variable toggles between the execution of the respective state-specific code within the loop.

Haf
- 11
- 1
checkInput
function for the paused mode and the unpaused mode, because there might be some controls which are only available in one mode or the other. ThecheckInput
function for the paused mode might do nothing more than looking ifP
is pressed and then setgameIsPaused = false
, but some games also place additionally functionality on the pause screen (in the 90s it was popular to have the pause screen as hidden input for cheat codes, modern games like to combine it with a menu screen). – Philipp Apr 23 '15 at 15:01checkInput
/render
/update
calls. – Nicholas Miller Apr 23 '15 at 16:47