- Home /
OnSerializeNetworkView with multiple clients, doesn't work when syncing a float?
I'm using network views to communicate between multiple clients and a server, and I've come across an issue.
The problem is that OnSerializeNetworkView only receives a value when sending from one client and receiving it on the server, other clients don't receive the value, when sending a Float. However if I change the OnSerializeNetworkView to send a Vector3, every client and the server receives updates from everyone else.
I'm using Network.Instantiate to instantiate a prefab, containing this script below and a networkView, that has the script as Observed and I've tested both using Reliable and Unreliable.
I'm not really sure what the problem is, but I've found a few others asking about problems like mine, and I'm wondering if anyone has any idea how to fix it.
I've boiled it down to this code snippet right here:
// Update is called once per frame
void Update ()
{
if (networkView.isMine)
{
Value += Time.deltaTime;
transform.position = new Vector3(Mathf.Sin(Value), transform.position.y, transform.position.z);
}
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
//float data = 0f;
Vector3 data = Vector3.zero;
if (stream.isWriting)
{
//data = Value;
data = transform.position;
stream.Serialize(ref data);
}
else
{
if (networkView.isMine == false)
{
stream.Serialize(ref data);
//transform.position = new Vector3(Mathf.Sin(data), Mathf.Cos(data), 0);
transform.position = data;
}
}
}
This example works when I use the current code, but when I use the code in the //comments then it doesn't work.
It might be because you are declaring an float and a gameobject with the same name. So you should get some type issues. Hope this helps :)
PS. Do you get any errors?
nope, that's not it, and no, I don't get any errors, neither on the server nor on the client...
And as I stated, it works using the same variable names, but sending a Vector3 ins$$anonymous$$d of a Float.
Answer by pyjamads · Aug 29, 2014 at 11:55 AM
I figured out my problem, the reason it behaved so differently between Vector3 and Float, was that the incoming value was never "stored" on the server, and apparently the was OnSerializeNetworkView works, is that all clients send their updates to the server, then the server deserializes, and then it writes out to all other clients.
problem and solution explained better here: client-to-client-serialization problem
Your answer
Follow this Question
Related Questions
What is Send rate? 1 Answer
Client to client Serialization 1 Answer
Can OnSerializeNetworkView Call in the client by Writing mode? 0 Answers
Can you tell OnSerializeNetworkView who the owner is? 3 Answers
texture in networked game 2 Answers