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:
The following is the output to the Console as you repeatedly press the Space bar key:
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:
On StateManager
:
Line 7: private IStateBase activeState;
activeState
variable will store a reference to a State which is an IStateBase
type of objectactiveState
variable currently has a value of null, meaning there is no reference to a State object stored yetLine 9: void Start()
Start()
method, executing line 11Line 11: activeState = new BeginState(this);
new
keyword instantiates the BeginState
objectBeginState()
constructor method is called, and passes the argument this
to the BeginState
constructorthis
keyword is a reference to this object, which is the StateManager
Component objectBeginState
is receiving a reference to the StateManager
object at line 11On BeginState
:
Line 10: public BeginState(StateManager managerRef)
managerRef
is assigned the reference of the StateManager
objectLine 12: manager = managerRef;
StateManager
reference that is stored in managerRef
, is assigned to the member variable manager
that was declared on line 8Line 8: private StateManager manager;
manager
stores a StateManager
type of objectmanager
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");
BeginState
constructor method, which was StateManager
line 11On StateManager
:
Line 11: activeState = new BeginState(this);
BeginState
object has been created and initializedBeginState
object is now assigned to the member variable activeState
Line 14: void Update()
Line 16: if(activeState != null)
if
statement says that if the value in activeState
is not equal to null, then executeactiveState
is storing a reference to a State objectactiveState
is now storing a reference to BeginState
, this if
statement is trueLine 17: activeState.StateUpdate();
StateUpdate
() method on BeginState
is called every frameBeginState
On BeginState
:
Line 16: public void StateUpdate()
StateManager
Line 18: if(Input.GetKeyUp(KeyCode.Space)
Line 20: manager.SwitchState(new PlayState (manager));
SwitchState()
method on StateManager
line 20manager
stores a reference to StateManager
PlayState(manager)
is being passed to the SwitchState()
methodBeginState
was instantiated, here we have a new PlayState
object being instantiated by using the new
keywordBeginState()
constructor method, the PlayState()
constructor method takes a parameter of a reference to StateManager
, which is stored in the member variable manager
PlayState
to initialize it's member variablesnew PlayState(manager)
is now a reference to the new PlayState
object just createdPlayState
reference that is actually passed to the SwitchState()
method on StateManager
StateManager
line 20On StateManager
:
Line 20: public void SwitchState(IStateBase newState)
PlayState
object reference and assigns it to the variable newState
IStateBase
type of objectIStateBase
interfaceLine 22: activeState = newState;
PlayState
reference stored in newState
is now assigned to activeState
I did a lot of explaining, but there were just the following basic things that took place after clicking on Play:
StateManager
object instantiated a BeginState
object and stored its reference in activeState
.BeginState
object waited for us to press the Space bar key to switch States.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.
18.226.163.229