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();
		}
	}

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:

  1. owner serializes and sends data
  2. server deserializes and applies data
  3. server serialized and sends data
  4. 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.