Time for action – modify StateManager

Edit the StateManager class to use the IStateBase interface. This allows the activeState variable to store all of the State class objects. Also add the code that does the switching to the next State:

  1. Modify StateManager as shown in the next screenshot.
  2. Remove the Debug.Log statement.
  3. Save all files.
  4. In Unity click on Play.
  5. Now press the Space bar key to cycle through the States.
    Time for action – modify StateManager

What just happened?

The following is the output to the Console as you repeatedly press the Space bar key:

What just happened?

The State Machine starts with BeginState being active. Pressing the Space bar key makes PlayState the active State. Pressing the Space bar again makes WonState the active State, and then pressing the Space bar key once more makes BeginState active again.

We now have a working State Machine. For the benefits a State Machine provides, there isn't much code involved to changing States.

Let us follow the code flow for switching States:

  • There are no States created as of yet
  • The StateManager script is a Unity script, therefore Unity instantiates (creates) the StateManager Component object

On StateManager:

Line 7: private IStateBase activeState;

  • The activeState variable will store a reference to a State which is an IStateBase type of object
  • The activeState variable currently has a value of null, meaning there is no reference to a State object stored yet

Line 9: void Start()

  • Unity then calls the Start() method, executing line 11

Line 11: activeState = new BeginState(this);

  • The new keyword instantiates the BeginState object
  • The BeginState() constructor method is called, and passes the argument this to the BeginState constructor
  • The this keyword is a reference to this object, which is the StateManager Component object
  • BeginState is receiving a reference to the StateManager object at line 11

On BeginState:

Line 10: public BeginState(StateManager managerRef)

  • This is the constructor for initializing member variables
  • The parameter variable managerRef is assigned the reference of the StateManager object

Line 12: manager = managerRef;

  • The StateManager reference that is stored in managerRef, is assigned to the member variable manager that was declared on line 8

Line 8: private StateManager manager;

  • The variable manager stores a StateManager type of object
  • Every time a new State is created, manager stores a reference of the StateManager class, because each State needs to be able to call the SwitchState() method on StateManager

Line 13: Debug.Log("Constructing BeginState");

  • This simply sends a text message to the Unity Console
  • This is needed temporarily so we can see the States changing when we press the Space bar key
  • The code block is now finished
  • Code flow now jumps back to the place that had called this BeginState constructor method, which was StateManager line 11

On StateManager:

Line 11: activeState = new BeginState(this);

  • The BeginState object has been created and initialized
  • A reference of the BeginState object is now assigned to the member variable activeState

Note

At this point, BeginState is now the active State.

Line 14: void Update()

  • This method is called every frame by Unity to execute its code block

Line 16: if(activeState != null)

  • This if statement says that if the value in activeState is not equal to null, then execute
  • Its checking to see if the variable activeState is storing a reference to a State object
  • If it isn't, the value stored will be null, like it was when we first clicked on Play
  • Since activeState is now storing a reference to BeginState, this if statement is true

Line 17: activeState.StateUpdate();

  • Using Dot Syntax, the StateUpdate() method on BeginState is called every frame
  • The code flow now jumps to line 16 of BeginState

On BeginState:

Line 16: public void StateUpdate()

  • This method is called every frame by the StateManager
  • There is no game to control yet, nor any game generated events
  • Therefore, we are simply checking for a press of the Space bar key to change to another State

Line 18: if(Input.GetKeyUp(KeyCode.Space)

  • Unity is checking whether we have pressed the Space bar key
  • When we do, then the the code block is executed

Line 20: manager.SwitchState(new PlayState (manager));

  • The Dot Syntax is used to call the SwitchState() method on StateManager line 20
  • The variable manager stores a reference to StateManager
  • The argument new PlayState(manager) is being passed to the SwitchState() method
  • Similar to when BeginState was instantiated, here we have a new PlayState object being instantiated by using the new keyword
  • Similar to the BeginState() constructor method, the PlayState() constructor method takes a parameter of a reference to StateManager, which is stored in the member variable manager
  • Code flow now jumps to line 10 of PlayState to initialize it's member variables

    Note

    The code flow logic for line 10 of PlayState is the same as line 10 of BeginState.

  • The argument new PlayState(manager) is now a reference to the new PlayState object just created
  • It is this PlayState reference that is actually passed to the SwitchState() method on StateManager
  • Code flow now jumps to StateManager line 20

On StateManager:

Line 20: public void SwitchState(IStateBase newState)

  • The parameter takes the PlayState object reference and assigns it to the variable newState
  • This variable stores an IStateBase type of object
  • Remember, all States implement the IStateBase interface

Line 22: activeState = newState;

  • The PlayState reference stored in newState is now assigned to activeState

    Note

    At this point, PlayState is now the active State.

Summarizing the code flow

I did a lot of explaining, but there were just the following basic things that took place after clicking on Play:

  1. The StateManager object instantiated a BeginState object and stored its reference in activeState.
  2. The BeginState object waited for us to press the Space bar key to switch States.
  3. The BeginState object instantiated a PlayState object and told StateManager to store the PlayState class reference in activeState.

Note that steps 2 and 3 just repeat for switching to any State, no matter how many you have.

Adding another State

You've probably noticed we haven't used LostState. Let's see how easy it is to include another State.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.163.229