- Home /
Problems changing NetworkStateSynchronization at runtime.
In my game the server creates starships for new clients when they join the server. Weapons on the ships are child objects that are instantiated via RPC calls that also provide a server-allocated viewID. One type of weapon is a drone that separates from the ship when launched (by removing it's parent, etc, via another RPC call).
Currently, the state synchronization for the drone prefab is set to unreliable in the inspector so that the drone can move on it's own after separation. I hate doing this, though, as it seems dumb to have a bunch of child objects receive state updates when they don't need to.
I want to set the state synchronization via the RPC call to separate the drone from the ship, but all attempts at setting this at runtime have failed. This is really no surprise as the script reference page for state synchronization says "Do not change state reliability on the fly after state synchronization has already happened". However, I feel like there has to be some way to get around this...
Answer by Jamora · Aug 10, 2013 at 05:57 PM
You don't actually have to serialize anything when OnSerializeNetworkView is called. You could do a quick fix with something like:
void OnSerializeNetworkView( /*parameters*/){
if(separated){
/*Do serialization*/
}
}
This way nothing should be sent over the network and the only overhead is of calling the function and checking the bool.
Because of my dislike for bool flag checking, I would instead have all the separatable ships attached to the mothership without a network view. Whenever one separates, I'd do a Network.Instantiate and send an RPC to everyone to disable the stationary one.
I'm not quite following you. I'm not currently using OnSerializeNetworkView since the state synchronization seems to handle transform serialization on it's own.
Are you saying that if I add an OnSerializeNetworkView function then this (automatic transform serialization) will not occur and I'll need to serialize that information manually?
If you're observing the gameobjet's transform with your networkView, then Unity will do the state syncing for you.
You can do a custom serialization by creating a script and setting that as the observed component of the networkView. (Drag and drop the title of the script into the observed slot of the networkView.) OnSerializeNetworkView will then be called sendRate times per second in that script.
Thanks Jamora, your answer prompted me to try something else that solved the issue. Posting answer below:
Answer by Afterthought · Aug 11, 2013 at 06:40 AM
The issue was solved (I think) by NOT setting the observed property of the networkView to the drone's transform by default. Instead, I set it to a random script on the object to keep it from sending state updates until separation. At separation I set the observed property to the transform and all is well.
Note that I tried setting the default observed object to None, but this results in some warnings on the client upon connecting to the server about state updates for observed objects that do not exist. Setting it to a random script on the object doesn't cause this warning to appear.
Your answer
