011. Foray into Photon - Part 10 - Creating our Disconnected state and wiring it into the game

The first thing we need to do is set up our Game.cs calls to our state so the state can handle them, then we will create our disconnected state and tell Game.cs to use it going forward. I have updated all the IPhotonPeerListener functions. They look exactly like the ones Exit Games uses only without the debug logging:

     public void DebugReturn(DebugLevel level, string message)
     {
        if (_listener.IsDebugLogEnabled)
        {
            _listener.LogDebug(this, message);
        }
     }

    public void EventAction(byte eventCode, Hashtable photonEvent)
    {
        _stateStrategy.OnEventReceive(this, (EventCode)eventCode, photonEvent);
    }

    public void OperationResult(byte operationCode, int returnCode, Hashtable returnValues, short invocId)
    {
        try
        {
            _stateStrategy.OnOperationReturn(this, (OperationCode)operationCode, returnCode, returnValues);
        }
        catch (Exception e)
        {
            _listener.LogError(this, e);
        }
    }

    public void PeerStatusCallback(StatusCode statusCode)
    {
        try
        {
            _stateStrategy.OnPeerStatusCallback(this, returnCode);
        }
        catch (Exception e)
        {
            _listener.LogError(this, e);
        }
    }

    public void Update()
    {
        _stateStrategy.OnUpdate(this);
    }

    public void SendOp(OperationCode operationCode, Hashtable parameter, bool sendReliable, byte channelId)
    {
        _stateStrategy.SendOperation(this, operationCode, parameter, sendReliable, channelId);
    }

As you can see, each place we normally would have done something because we received a message through the peer, we now pass on to the state. The state will then determine if we need to process the message and if so, how.

Now to create our Disconnect state. It is a very simple state, it does nothing when asked to send an operation or to update. The other functions all call the error logs we created earlier.

using AegisBornCommon;

public class Disconnected : IGameState
{

    public static readonly IGameState Instance = new Disconnected();

    public GameState State
    {
        get { return GameState.Disconnected; }
    }

    public void OnEventReceive(Game gameLogic, AegisBornCommon.EventCode eventCode, System.Collections.Hashtable eventData)
    {
        gameLogic.OnUnexpectedEventReceive(eventCode, eventData);
    }

    public void OnOperationReturn(Game gameLogic, AegisBornCommon.OperationCode operationCode, int returnCode, System.Collections.Hashtable returnValues)
    {
        gameLogic.OnUnexpectedPhotonReturn(returnCode, operationCode, returnValues);
    }

    public void OnPeerStatusCallback(Game gameLogic, ExitGames.Client.Photon.StatusCode returnCode)
    {
        gameLogic.OnUnexpectedPhotonReturn((int)returnCode, OperationCode.Nil, null);
    }

    public void OnUpdate(Game gameLogic)
    {
        // Do nothing on updates because we are disconnected.
    }

    public void SendOperation(Game gameLogic, AegisBornCommon.OperationCode operationCode, System.Collections.Hashtable parameter, bool sendReliable, byte channelId)
    {
        // Do not send operations because we are disconnected.
    }
}

So now we have our disconnected state and we are almost back to where we were. It feels like a ton of work just to be right back where we started, but this is a very good setup. The time we take setting up this project the first time, means less time will be spent when we need to add new states or messages.

Next up we will get WaitingForConnect and CharacterSelect states up and running!

Comments

I've completed up to part 12 and I'm getting few error that do not make any sense to me. Sorry if these are stupid questions to be asked, as I am very new to this.
1. I am getting an error in Login class, saying "_stateStrategy does not exist". It comes from the minor change that I did as the tutorial suggested with, public "GameState State method" implementation on Login class. Why did you wanted to get the state from the Login class, since we're already getting it via the Game class.
2. what parameters should be sent with OnGUI "SendOp" in Login class, which is the method used to send an operation to the Server.

for the _stateStrategy error, I renamed stateStrategy to _stateStrategy in Game.cs so you will need to make that change.

The next several steps are to be completed all at once. I broke up the parts, but they must all be completed before it will work together.