Don't understand a line in a state machine

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;
	}
}

Well that’s all necessary :wink: The StateManager class is your actual statemachine which has a reference to the current state (stored in activeState). The BeginState class (or any other state class for this matter) doesn’t know to which manager it belongs. In OOP it’s usually the rule that everything a class needs to know about should be passed as argunent in it’s constructor. So when you create a BeginState / PlayState you have to pass the reference to the manager, so the state object has a reference to the manager that is using this state.

This manager reference might be used inside that state class to switch to another state by using the managers “SwitchState” method.

edit

I think you’re confusion comes from the fact that the line in question does two things at once. Let me split that line up:

    // Create a new instance of the PlayState class. This instance need a manager
    // reference internally to be able to change the state to a different state
   IStateBase newStateObject = new PlayState (manager);


    // Here we call SwitchState of your manager and pass the newly created instance.
    manager.SwitchState(newStateObject);

This is exactly the same as your line:

    manager.SwitchState(new PlayState (manager));