Desgin assistance for networking.

Hey all–

I’m trying to figure out how to organize this; I think now would be the appropriate time for trying out some sort of design pattern for the first time;

Here’s whats going on.

In converting my single player game to networked, I have come up with this way of managing my code:

function MessageSomething (var : Object)
{
     if (isNetworkedGame  Network.isServer)
     {
         networkView.RPC("Something",RPCMode.All,var);
     }
     else
     {
        Something(var);
     }
}

@RPC
function Something (var : Object)
{
    //do something
}

This has got to be one of the worst ways to program this. For example, I want to add a bluetooth section, which means I need a

else if (isBluetooth)
{
//handle bluetooth call
}

And don’t get me started when I want to add seperate protocols for Android, and iPhone.

I was thinking of creating some sort of a Protocol class-- so all I had to do was something like:

var myObject : Object = Object();
var protocol : Protocol = BlueToothProtocolIPhone();
protocol = BlueToothProtocolAndroid();
protocol = NetworkProtocol();

protocol.Message("Something",myObject);

And depending on which protocol I initialized, that would handle whether to call the function directly, call it through an RPC, or do something with bluetooth plugins.

However, I was wondering if there was some sort of a design pattern I might be able to use; does someone have some suggestions?

Working on something similar and found that using the Observer pattern with Delegates + Interfaces in C# are great for this. Not sure if delegates can can be used in UnityScript or iPhone, but interfaces can and you don;t need Delegates for the pattern, they just make it a little cleaner.

http://en.wikipedia.org/wiki/Observer_pattern#C.23