Saturday, 11 April 2015

Unity Networking: Playing on the server

Having to open two instances whenever testing a networked application is awkward.

Two questions arise when trying to "play on the server":
  1. Can we setup as both client and server within the same instance? The answer is NO. Beyond the negative result, "Client" and "Server" roles are interchangeable for most practical purposes.
  2. Can we RPC from the server to itself? Somewhat surprisingly, the answer is, again NO. More generally it appears that you cannot unicast from a network player to the same network player.
Short of re-implementing all our input interface to invoke server instances locally, what do we do? Reflection will help overcome this hurdle:

private void ClientRequest(string name,params object[] args){

        
if (Network.isServer
            typeof(TargetType).GetMethod(name).Invoke(avatar,args);
        else 
            networkView.RPC (nameupstreamargs);

}

In the above example...
  • TargetType is the type of the object that you wish to invoke.
  • networkView is the network view that you would normally send your RPC from.
  • Remember using System.Reflection.
More powerful than MonoBehaviour.Invoke( name, delay ).

No comments:

Post a Comment