- Home /
Strange Variables in OnSerializeNetworkView?
The data I'm receiving is not the data I should be receiving using OnSerializeNetworkView. Is there anything wrong with this code or anything that could end up in weird variables?
function OnSerializeNetworkView (stream : BitStream, msg : NetworkMessageInfo)
{
var pos = transform.position;
var rot = transform.rotation;
var newPos = pos;
var newRot = rot;
if (stream.isWriting) {
if (isactive)
{
pos = transform.position;
rot = transform.rotation;
newPos = pos;
newRot = rot;
Debug.Log("isactive: " + newPos);
stream.Serialize(newPos);
stream.Serialize(newRot);
}
} else {
if (!isactive)
{
stream.Serialize(newPos);
stream.Serialize(newRot);
Debug.Log("Recieve " + newPos);
pos = newPos;
rot = newRot;
}
}
if (!isactive)
{
transform.position = pos;
transform.rotation = rot;
Debug.Log("Transform equals: " + pos);
}
}
Thanks!
Answer by Bunny83 · Aug 21, 2012 at 04:19 PM
I've read most of the comments here and i would like to add some general hints:
When using the network, make sure your application runs in background. Otherwise the server (or client) won't respond to any package sent when the application doesn't has the focus. That's the most common mistake. You can also set this via scripting at runtime.
"Automatic syncing", either by setting a Transform as observed object, or by using OnSerializeNetworkView, only the object owner can send any data and all others can just receive the updates. Owner referes to the NetworkViewID owner. This specifies who owns the object with that ViewID. The owner can't be specified manually. Whenever someone creates a new ViewID, this player will be the owner. Network.Instantiate creates a ViewID for all NetworkViews that are within the object and it takes care of sending them to all others so they are set properly on all peers. So the player that calls Network.Instantiate will own the object and he's the only one who can move the object.
When using syncing with an observed Transform, Unity will only send an update when the position / rotation has changed. When using OnSerializeNetworkView you can send any arbitrary data, so deltaCompression will probably be disabled.
The SendRate (via scripting) specifies the update interval how often a package is send and therefore how often OnSerializeNetworkView will be called.
If you want to disable syncing of a certain NetworkView, just set the stateSynchronization property to Off. In this case, no updates are send.
It's vital that all receiving peers deserialize exactly the same data that you send. You change the data you're sending depending on your isactive variable. When this variable is changing on the sending peer, the receiving peer doesn't know about that.
One way is to send the isactive first and decide on that what and if additional data will follow.
function OnSerializeNetworkView (stream : BitStream, msg : NetworkMessageInfo)
{
var pos = transform.position;
var rot = transform.rotation;
var active = isactive;
stream.Serialize(active); // always send / receive the active flag
if (active)
{
stream.Serialize(pos); // only send / receive pos / rot when active
stream.Serialize(rot);
}
if (active && stream.isReading)
{
transform.position = pos;
transform.rotation = rot;
//isactive = active; // if you need it also on the other peers.
}
}
THAN$$anonymous$$ YOU THAN$$anonymous$$ YOU THAN$$anonymous$$ YOU THAN$$anonymous$$ YOU!!!
If I could vote 50 thumbs up I would!!! THAN$$anonymous$$ YOU SIR, YOU SAVED THIS GA$$anonymous$$E FRO$$anonymous$$ COLLAPSE! :D
@Bunny83, +1
just interested in what was the exactly error that i missed in this issue 8)
Just the code above: I had to always send the position if active, not just if (stream.isWriting), but only apply it if stream.isReading. I think the position was being reset because of an inconsistency in sending/receiving, but I'm not exactly sure.
Answer by ScroodgeM · Aug 10, 2012 at 08:57 PM
if this happens on two different clients connected to server, then pay attention what happened on server. server in this case is not just resends data, it applies to instantiated object, then reads it again from object and sends to other players. so if you have some differences in scene that could change position of object - that's why your issue happens.
There should be no differences. And I should only be sending data from 1 client, and receiving on the other. And I removed all movement if the client is not sending.
do you use server between clients? or just one of these clients is server? if server between, just check what server receives and what it sends to other clients. methinks you will be surprised 8)
... I've already done that -- I'm sending the correct position through but the server is receiving something different. There are no differences in the scene and the movement was removed from all except 1 client.
i'm 99.9999% sure that you can't send from client something that can be received by server different. this is only network transfer that can't change data. somewhere (rather on server) data is changed. did you check this data directly in OnNetworkSerializartion method? and on client too?
That's really strange... When I run the server on the Editor it works perfectly fine, but when I run it from the built client it starts sending and receiving the wrong data. What could be causing this?