Why do I need a game loop with states, instead of init the next state in current state directly?

by mmmaaa   Last Updated August 10, 2018 02:05 AM

For example, it is common to see games with game loop and states:

stateChanged(){
  switch(state){
    STATE.PLAYER_SELECT_CHARACTER:
      this.currentController=new PlayerSelectCharacterController();
      break;
    STATE.CHARACTER_MOVE:
      this.currentController=new CharacterMoveController();
      break;
  }
  this.replaceController(this.currentController);
}

class PlayerSelectCharacterController{
  .
  .
  .
  characterSelected(int charIndex){
    this.globalData.charIndex=charIndex;
    this.globalData.state=STATE.CHARACTER_MOVE;
    this.parentController.stateChanged();
  }
}

but my question is, why don't I init the next state at last states directly? I think it is better, especially when I need to pass parameters to next states:

class PlayerSelectCharacterController{
  characterSelected(int charIndex){
    this.parentController.replaceController(new CharacterMoveController(charIndex));
  }
}    


Related Questions


fall-through switch for executing a sequence of steps

Updated November 21, 2018 14:05 PM

State Design Pattern

Updated July 05, 2016 08:02 AM

How to reduce a switch in a switch statement?

Updated July 14, 2015 13:02 PM