010. Foray into Photon - Part 09 - Adding IGameState and IGameListener to the game

So we have our game states and our listener ready, but we don't reference them anywhere. We are now going to make some modifications to the Game.cs file that will make these calls.

First we add 2 new properties and a new constructor:

    private readonly IGameListener listener;

    private IGameState stateStrategy;

    public Game(IGameListener listener)
    {
        this.listener = listener;
        this.stateStrategy = Disconnected.Instance;
    }

Next we go into Login.cs and modify our Start function to say:

        _engine = new Game(this);

Now we can add some error handling to our Game.cs file:

        public void OnUnexpectedEventReceive(EventCode eventCode, Hashtable eventData)
        {
            this.listener.LogError(this, string.Format("unexpected event {0}", eventCode));
        }

        public void OnUnexpectedOperationError(OperationCode operationCode, ErrorCode errorCode, string debugMessage, Hashtable hashtable)
        {
            this.listener.LogError(this, string.Format("unexpected operation error {0} from operation {1} in state {2}",  errorCode, operationCode, this.stateStrategy.State));
        }

        public void OnUnexpectedPhotonReturn(int photonReturnCode, OperationCode operationCode, Hashtable hashtable)
        {
            this.listener.LogError(this, string.Format("unexpected return {0}", photonReturnCode));
        }

This gets us all caught up to where we need to be so we can begin to implement our states. We will cover our basic disconnected state and the functions in Game.cs that we need to modify in our next segment. After we get our states set up I will be posting the code to github so that you can follow along if needed.

Comments

xcalpro's picture

Disconnected.Instance is giving me an error saying "The name `Disconnected' does not exist in the current context" Am I missing a 'using' statement?

No, you need to follow part 10 first, which is the next section. Parts 8-12 were meant to be completed together. Until you finish part 12, you will have errors.