12
\$\begingroup\$

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?

\$\endgroup\$
20
\$\begingroup\$

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();
\$\endgroup\$
  • \$\begingroup\$ You might want to have a different 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. The checkInput function for the paused mode might do nothing more than looking if P is pressed and then set gameIsPaused = 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). \$\endgroup\$ – Philipp Apr 23 '15 at 15:01
  • 1
    \$\begingroup\$ While generally I'd agree with your answer, I think it should be explicitly stated that sometimes it is perfectly valid to run some updates within the paused state. For example, animating menu items or perhaps some audio management. The big idea is that the pause condition allows you to branch into another set of checkInput/render/update calls. \$\endgroup\$ – Nick Miller Apr 23 '15 at 16:47
  • \$\begingroup\$ The only thing that pausing the game should change is that the "update world" bit should be skipped. All other functionality should probably keep as normal. \$\endgroup\$ – Mooing Duck Apr 23 '15 at 17:16
  • 4
    \$\begingroup\$ I tried to keep the answer as simple as possible, because this is very basic stuff in game programming and i did not wan't to confuse the questioner. \$\endgroup\$ – Katu Apr 23 '15 at 17:22
  • 1
    \$\begingroup\$ @user3730788: A branch that executes once per game loop (i.e. less than 100 times per second) will have no discernible performance impact, not even if it's mispredicted every time. \$\endgroup\$ – Ilmari Karonen Apr 23 '15 at 20:21
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.