Synchronisation from client to server

Hi all,

I’m developing a network application and I experience a trouble in the synchronization from server to client.

When I move an Object on the server side , this Object move on the client side. But when I move an Object on the client side it doesn’t move because the position is synchronize from the server.

I would like to know How I can make a synchronization server/client and client/server.

I found this article : NetworkView Control - Questions & Answers - Unity Discussions

But I really don’t have a lot of knowledge in networking and I don’t really understand where I have to put this code?

I define the network like this :

if(server)
   { 
      Network.InitializeServer(4, listenPort, true);
      for(var obj in FindObjectsOfType(GameObject))
      { 
         obj.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
      }
   }

   else
   { 
      Network.Connect(remoteIP, listenPort);
   }

So My question is : Is my definition of the network good?

How can I allowed the synchronization client/server and where have I to put the code.

Thank you in advance for all the advice you could provide me because it’s very difficult to find informations about this subject.

I had this problem a while back. The client that allocates the viewID is recognised as the owner and send all updates to the view. You can simply make the client allocate the viewID from the start but then the server won’t be able to move it. If you want both client and server (and also multiple clients) to be able to take control of an object you have to reallocate the viewID at runtime. You can not have two clients moving the object at the same time but they can take turns. This is the code I used, these two functions need to be on a script attached to the object, and becomeOwner() on needs to be called by any client that wants to start manipulating the object.

function becomeOwner()
{
	var viewArray = this.gameObject.GetComponents(NetworkView);
	var objView : NetworkView = viewArray[0];
	var newObjID = Network.AllocateViewID();
	
	networkView.RPC("changeOwner", RPCMode.Others, newObjID);
	
	objView.viewID = newObjID;
}

@RPC
function changeOwner(objID : NetworkViewID)
{
	var viewArray = this.gameObject.GetComponents(NetworkView);
	var objView : NetworkView = viewArray[0];
	objView.viewID = objID;
}

Note this will only change the first networkView on an object, if there are multiple views on an object you will have to do this on each one.

Bear in mind this is an insecure way of doing things, if you give clients control over the network views they may use it to do illegal things, ie. cheating. The secure method is for the client to send their controls to the server and have the server move the object. This means the server can check if moves are legal before doing them.