- Home /
Client can't serialize to client
Hi,
I'm completely lost with this one.
I have a GameObject A who does have a NetworkView component. On Start A calls Network.Instantiate in order to instantiate a GameObject B in every other networked clients.
Then I use OnSerializeNetworkView on A to modify some properties of B. The NetworkView Component is set with the script in charge of OnSerializeNetworkView.
The problem is that only the host receive informations. When I only have two players (a host and a client) everything is fine, every players can see modification on each other's object. But when I have more than two players (say a host and two clients) every clients can see host's object modifications (and viceversa) but a client can't see another client's object modification.
Example of my code (very basic) :
private void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
if ( stream.isWriting )
{
int color = my_color;
stream.Serialize(ref color);
}
else
{
int color = 0;
stream.Serialize(ref color);
Debug.Log("receiving: " + networkView.viewID + " -- " + color);
// will log in client<->host but not in client<->client
}
}
Is there a trick to know?
Answer by asafsitner · Aug 28, 2012 at 09:57 AM
What happens is that your ObjectA has a different viewID than the instances of ObjectB that are created on everyone's machines (including the machine that called the Network.Instantiate).
Network.Instantiate allocates a new ViewID and sets the player who called it as the owner of that viewID. Now, serialization goes from one NetworkView to another based on the NetworkViewID of that NetworkView.
So if you try to serialize data from ObjectA who has, say, viewID of 2, the network will try to send that data to all other objects with the same viewID (2), but not to ObjectB who has a newly-allocated viewID of 3 (courtesy of Network.Instantiate).
To remedy this problem you'll have to serialize the changes from your copy of ObjectB, which you are the network owner of, to the other clients. Or make sure that the viewID of all the copies of ObjectB match the viewID of ObjectA by implementing your custom Instantiate RPC.
Don't hesitate to tell me if the answer is not clear enough. :)
Hey thank you for that answer. It's pretty clear... but my question seems not :) In fact only ObjectA has a NetworkView... ObjectA checks ObjectB state to send it over network via Serialize. ViewID numbers are the same for the same object on every clients. I'm lost. Feel like I will cry :)
I see. Who owns ObjectA?
The BitStream will only return true to isWriting on the object that belongs to the owner of the viewID.
You can just post your solution as answer and accept it, there's no need to delete the thread. Someone in the future might run into a similar problem.
@asafsitner I have re-opened it here : http://answers.unity3d.com/questions/311184/client-to-client-serialization.html because I thought it was working but apparently not. I want to cry :/
Cheer up! Synchronizing stuff over the network correctly is what kept me busy in the last year or so, and I still mess it up occasionally. :)
Your answer
Follow this Question
Related Questions
game objects and meshes not appearing 1 Answer
Get NetworkPlayer from ViewID 1 Answer
Photon - How to name a player? 0 Answers