Game Development Reference
In-Depth Information
vious time the
Update()
loop ran. By checking each frame if the
GameState
has changed, we can detect when to load a new level as shown
in the following code:
public eGameState gameState;
private eGameState _prevGameState;
While we don't want the user to be able to set
prevGameState
ever from
the inspector, we allow the GameState to be adjusted by the user for debug-
ging purposes.
7. In the
Start()
method of this class, we initialize
GameState
and
pre-
vGameState
to the same value as shown in the following code. With an
initial value of
eGS_MainMenu
, this corresponds to the main menu scene
(which is the one we will default to when we load the
MAIN
scene). How con-
venient! Note that since both the previous and current GameStates are the
same,
GameMgr
will not try and load a new scene file right away:
GameState = eGameState.eGS_MainMenu
prevGameState = eGameState.eGS_MainMenu;
8. We create a public method to allow other systems to set the state as shown
in the following code. By ensuring that we always use this function rather
than assigning to state directly, it will allow us to change state to private later
on (once the game is done) without having to change the code elsewhere:
public void SetState(eGameState gs)
{
GameState = gs;
}
9. In the
Update()
loop, the code will check if
GameState
is not equal
to
prevGameState
. When this happens, it means that in this frame, the
GameState
was changed by another system and that
GameMgr
should
change levels. To perform the state change, the code will invoke a custom
private method
ChangeState()
as shown in the following code: