Friday, June 17, 2011

Foray into Photon - Part 7 - IGameState

Last time we left off talking about the state pattern, but my title said strategy/state pattern. The reason is because the two patterns are closely related to each other. After reading the Design Patterns book, also called the "Gang of Four" or GoF book because of the 4 authors that wrote it, I determined that they are using the State pattern. The idea behind the State pattern is that the program will constantly change states. The idea behind the Strategy pattern is that you will use one strategy or algorithm and it won't change very often if at all unless the user or developer wants it to change. Most people will give the idea of a sort as being a strategy, the developer will chose which sorting strategy they want to use. A state however, changes frequently, hence why I call these states rather than strategies. 

Now on to the code. You will want to open your Unity3d project and create a folder called GameStates.

First we need to create our list of states as an enumeration. It's simply a file that contains a simple enum:

public enum GameState
{
    Disconnected,
    WaitingForConnect,
    CharacterSelect,
    CharacterCreate,
    WorldEntered
}

Next we are going to create our interface IGameState. This does nothing more than define the functions that all game states will use. Just like the Exit Games version, we will have a few functions, OnEventReceived, OnPeerStatusCallback, OnOperationReturn, OnUpdate, and SendOperation. The code for this class looks like this:

using System.Collections;
using AegisBornCommon;
using ExitGames.Client.Photon;

public interface IGameState
{
    GameState State { get; }

    void OnEventReceive(Game gameLogic, EventCode eventCode, Hashtable eventData);

    void OnOperationReturn(Game gameLogic, OperationCode operationCode, int returnCode, Hashtable returnValues);

    void OnPeerStatusCallback(Game gameLogic, StatusCode returnCode);

    void OnUpdate(Game gameLogic);

    void SendOperation(Game gameLogic, OperationCode operationCode, Hashtable parameter, bool sendReliable, byte channelId);
}

With this in place we are going to go ahead and end this segment. There are other things we need in place before we can implement these states. We need to modify the Game.cs file and create our game listener which will be a mono behavior. Stay Tuned.

2 comments:

Ben Rodgers said...

I ran into a snag here. Where you've got the using AegisBornCommon reference. It's stating that the namespace cannot be found. I've double and triple built the Common project and moved the dll into the plugins folder. Not sure what's happening because if I remove the reference it additionally errors on SendOperation, so it must be looking at some part of the reference. Thanks again for the great tutorials.

Unknown said...

So I believe you are hitting the same snag I hit in a later part of the tutorial. I had to go into the project properties for the Common project and make it set to use .NET Framework 3.5. I was originally using 4.0 and Unity3d doesn't like .NET Framework 4.0 compiled binaries.