- Home /
Client to client Serialization
Hi,
I have some troubles to synchronize players thanks to `OnSerializeNetworkView`. I have read a lot of documentation about this but nothing helped me in this specific case.
I have set a `TextMesh` (let's call it `text_entry`) in my scene and players can interact with it by entering a number.
I have another `TextMesh` (called `text_reflect`) that is instantiated thanks to `Network.Instantiate` at the beginning of the game. `text_reflect` has a `NetworkView` component and a script is attached to this component. It's a simple monobehaviour script with only `OnSerializeNetworkView` filled.
The method looks like this:
private void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
int stream_value = 0;
if ( stream.isWriting )
{
stream_value = int.Parse(GameObject.Find("text_entry").GetComponent<TextMesh>().text);
stream.Serialize(ref stream_value );
}
else if ( stream.isReading )
{
stream.Serialize(ref stream_value );
GetComponent<TextMesh>().text = stream_value .ToString();
}
}
As you can see I want to reflect over the network the value of text_entry by changing text_reflect.
This actually works for a host <=> client serialization but not for client <=> client ... Values coming from host are correctly updated but when it comes from a client to another client the `stream.isReading` context is not even executed.
Of course this is just an example but I can't manage to make it working.
Any help would be appreciated!
After answer the code should look like this:
private void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
int stream_value = 0;
if ( stream.isWriting )
{
if (networkView.isMine)
stream_value = int.Parse(GameObject.Find("text_entry").GetComponent<TextMesh>().text);
else if ( Network.peerType == NetworkPeerType.Server ) // here server relay data
s = int.Parse(GetComponent<TextMesh>().text);
stream.Serialize(ref stream_value);
}
else
{
stream.Serialize(ref stream_value );
GetComponent<TextMesh>().text = s.ToString();
}
}
Now to more specific questions:
I presume that 's' is a class field and is initialized prior to OnNetworkSerialize.
Who instantiates `text_reflect`? Does every client have one copy that they own, replicated on the other clients' machines?
About 's'; it's a typo I will change in the question. 's' was in fact 'stream_value'.
Another object instantiate text_reflect. Every client have one copy they own and correctly replicated on other clients. Every ViewID are replicated.
@asafsitner When you try something as simple as this example... does it work for you?
Answer by ScroodgeM · Aug 31, 2012 at 10:45 PM
server not just relaying data. it receives data like client from owner, and then sends like owner to other clients. so your data chain looks like:
owner serializes and sends data
server deserializes and applies data
server serialized and sends data
remote clients deserializes and applies data
in your case (1) works but (4) fails. sine (1) vs (2) and (3) vs (4) should usually work, your miss rather between (2) and (3). so debug server about does it works ok without exceptions and what does it reads and sends.
So you mean server should not only read but also write in stream even for network view he doesn't own? I thought it was not possible to write in a stream you don't own... I will try anyway. Thank you.
only server can write (and it always do it) to serialized data.
dude you just saved me from going insane .... Thx so much ... :) Couldn't for the life of me figure out why my client to client communication wasn't working with Floats but it worked with Vector3, turns out, I was storing the Vector3 value on the server in the Transform.position, to have some visual feedback to my problem, but the float value, didn't get stored server side, so when the server 're-routed' the variable, it just contained 0 ....