game states

Jul 22nd, 2015
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. game states
  3. in your game you can use special variables that you can use to switch between states.
  4. these states can be used to redirect flow in/out of the game "engine", in/out of a menu, or whatever.
  6. int G=1;
  7. while(bRunning) {
  8. if(G==1) {
  9. update game physics
  10. redraw game
  11. }
  13. if(G==2) {
  14. update menu physics
  15. redraw menu
  16. }
  17. }
  19. By setting G=1, the game runs normally. By setting G=2, it seamlessly switches to the menu. Importantly, the game is just suspended. It hasn't gone anywhere. The data is all still there. It's physics and drawing just stop running. You can resume it any time by setting G=1.
  21. Game states can be useful for menus. Or any time you want to switch between distinct parts of your game.
  23. You can do them more cleverly than this, like if you want to fade between game and menu, or time to lead in/out. But this should be enough to explain the basic idea.
  25. A state system can be used within a menu itself, to switch between submenus and display different settings. Some people like to use a stack structure, instead of a single state variable, so that POPing a state automatically returns to the previous state. Stack based state systems can be used to add layers into the foreground like a series of dialogue boxes.
RAW Paste Data