- Home /
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 : http://answers.unity3d.com/questions/289324/networkview-control.html
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.
Answer by Dunkhan · Apr 02, 2013 at 04:53 PM
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.
Hi Dunkhan, Sorry for the delay of my end, I've been rather busy... Thank you so much for your answer.
I tried your code but I have two error :
View ID AllocatedID: 0 not found during lookup. Strange behaviour may occur
Received state update for view id' AllocatedID: 0' but the NetworkView doesn't exist
I can move the object from the client side but it does not synchronize on the server side.
Have you any ideas of where this where this problem comes?
Thanks you again for your help.
Regards
I am sorry but it is difficult to work out what those errors mean specifically without more information. Those errors you are receiving relate to update information being sent from a network view on one side, that does not exist on another. Did you copy the code to both the client and the server? Have you set any view ids in the inspector?
It may be easier for you to simply use the second method. Put a script on the player and serialise all their control input to the server, and have only the server move the object.
Consider using pastebin to post sections of code and more detail from the error messages. Also a brief description of what your project is supposed to do in the end often helps in working out which network solution will suit you.
Your answer
Follow this Question
Related Questions
How fast do Syncvars synchronize? 1 Answer
Multiplayer Animating 2 Answers
Networking Sync SetActive Not Working 0 Answers
Sync animaor of child prefab 0 Answers
network communication: How to make sure that information is in sync without server? 0 Answers