Hi
I am Learning from a book “Learning C# by Developing Games with Unity 3D Begginers Guid”
In chapter 8, he is explaining how to build a state machine.
I dont understand this line of code in the BeginState class:
manager.SwitchState(new PlayState (manager));
why manager after playstate?
manager.SwitchState - > access that method
new PlayState → pass this to SwitchState as the new agruments and equal this instance to activState
But why I need (manager)); again ?
using UnityEngine;
using Assets.Code.Interfaces;
namespace Assets.Code.States
{
public class BeginState: IStateBase
{
private StateManager manager;
public BeginState(StateManager managerRef)
{
manager = managerRef;
Debug.Log ("Constructing BeginState");
}
public void StateUpdate ()
{
if (Input.GetKeyUp (KeyCode.F))
{
manager.SwitchState (new PlayState (manager));
}
}
public void ShowIt ()
{
}
}
}
---------------------------------------
-------------------------------------
using UnityEngine;
using Assets.Code.States;
using Assets.Code.Interfaces;
public class StateManager : MonoBehaviour
{
private IStateBase activeState;
void Start ()
{
activeState = new BeginState(this);
}
void Update ()
{
if (activeState != null)
activeState.StateUpdate ();
}
public void SwitchState (IStateBase newState)
{
activeState = newState;
}
}