title: What are good techniques to handle state and state change in game development?
body: <p>I'm an experienced developer new to game development, and I have made a reasonably simple but working 3D platformer game and am looking for ways to make the architecture more scalable.</p>
  
  <p><strong>What is a good way to handle state in game development?</strong><br>
  A very naive approach is to have multiple booleans like:</p>
  
  <pre><code>time_stopped = True
  time_slow_motion = False
  level_loaded = True
  level_paused = True
  level_completed = False
  </code></pre>
  
  <p>This is very inflexible since a developer error could lead to <code>time_stopped = True</code> and <code>time_slow_motion = True</code> at the same time, which would destroy game logic.</p>
  
  <p>I'm looking for a better approach in the direction of:</p>
  
  <pre><code>class TimeState:
      STOPPED = 0
      SLOW_MOTION = 1
      NORMAL = 2
      current_state = 0
  
      def isStopped(self):
          if current_state == self.STOPPED: return True
  </code></pre>
  
  <p>But how would I handle multiple groups of state in a good way? I could create a <code>LevelState</code> which had <code>LOADED</code>, <code>PAUSED</code> and <code>COMPLETED</code> states. But how do I handle overlapping groups like when <code>LevelState</code> and <code>TimeState</code> <code>PAUSED</code> should always be the same?
  Is there a good design pattern or a good practice on how to solve state management?</p>
  
  <p><strong>What is a good way to handle state change?</strong><br>
  My thoughts are to either give the state object eg. <code>LevelState</code> callback functions to execute on any given state change - or to poll the state object each game loop like <code>if level_state.changedTo() == LevelState.COMPLETED: doStuff()</code>. Are there better ways to handle this? Message queues/events? I don't want to embed logic into the state objects.<br>
  I'm also interested in if one should handle both <code>changedFrom</code> and <code>changedTo</code> events, and what would happen if logic triggered by <code>changedFrom</code> changes the state of the object before the <code>changedTo</code> logic is triggered - who wins?</p>
  
  <p>I don't want implementation/language specific answers but tips on a good way to think architecturally wise.</p>