I'm in the process of redesigning some of my state management code (there are several features I've grown to dislike in practice..) which was based on this tutorial and others. The issue I'm having right now has to do with state transitions.
Where and when should state transitions occur? Should they be executed immediately, or should they be delayed to the end of the current frame, after the current state has finished its work for that frame?
Let's say my states can implement
resume methods, among others that are not relevant for this question.
My thoughts so far are the following:
If the transition is done at the end of the frame, then when I pause the game I might see an update during that same frame I clicked to pause. The Pause State will only appear after the current state has executed the rest of the frame (events, update and render). Perhaps that extra update should not have been done.
If the transition is done immediately I have to be careful to not to delete the current state while it is still executing its method, let's assume the
handleEventsmethod. But even so, if I were to pause a game in the
handleEventsfunction, then the game would immediately pause and the game state
updatefunction would not be executed that frame.
The second options is more appealing to me, because as a player I would probably notice that extra bit of movement after pressing to pause the game. What if the player died during that frame? The delayed state change would be unwarranted. But then, the game state itself should probably disable updates if a game pause was requested.
On the other hand it might make more sense to delay state transitions to after the current state has fully executed its functions for the current frame. Maybe the Pause state should be a sub-state of the Game state and not a state of its own.
Is it simply a matter of preference or is one of the approaches clearly superior for a different reason?