- Home /
Networking: how to sync NetworkViewIDs for level objects on peers?
I think this is a common issue.
I have a level with game objects. How am I supposed to load this level on remote peers so that game objects with NetworkView components were in sync?
To sync NetworkViewIDs you should use Network.Instantiate() or write custom instantiation code which will use Network.AllocateViewID();
I do not like Network.Instantiate because all my objects are already instantiated when level loads. Am I supposed to destroy game objects and recreate them with Network.Instantiate? This is stupid.
And how can I use AllocateViewID? I should invoke an RPC call to send my IDs and to tell clients which game objects these IDs are related to. And how can I specify an exact object via RPC?
EDIT: Do you have an idea how Network.Instantiate internaly works? This method should use an RPC call to send NetworkViewID to all connected peers and how it tells what object to instantiate?
I have a related question. When a new client connects to the server, how do you tell the client to load all the game objects already Network.Instantiated on the server, prior to connect?
I have used a C# version of this code: http://unity3d.com/support/documentation/Components/net-NetworkLevelLoad.html
However, the load is not happening.
@Swift_On_Fire: It's not really related and can't be answered that quick. In the OP case all NetworkViews that are part of a scene already have unique IDs. There are different versions of NetworkViewIDs. Beside the usual ones there are also scene-based view ids which are automatically in synch when you load the same scene. Usually when you send the loadlevel RPC as buffered RPC it should be the first thing that a new client is executing. All other things that has happend before the new client has connected are executed in the same order as long as they has been sent via buffered rpcs. Network.Instantiate uses a buffered RPC internally, so all those instantiate calls should be executed automatically.
If you're not familiar with the concept i strongly recommend to do some simple tests on your own. Networking can get really complex and it's hard to help anyone without having the full project.
If you think you really have a clear question that can be answered with a clear answer, post a real question ins$$anonymous$$d of commenting on this one.
Just a last hint: You don't have to use Network.Instantiate. You can handle the instantiation yourself, but you have to keep a list of all objects that have been created dynamically and tell a new client about them via RPCs, but as already said that's getting by far too complex for a short answer.
@Bunny83 Thank you for the suggestion. I do have a lot to learn about Unity networking. I did not realize that buffered RPCs get executed on a new client that way. I will go learn about buffered RPCs.
As for posting, I had posted the question prior, but no one answered.
Answer by PrimeDerektive · Jan 08, 2011 at 05:41 AM
Network.Instantiate works fine, if you clean up all the buffered instantiations. Network.Destroy() does not get buffered like Network.Instantiate, but you could just put a Network.Destroy() call in a buffered RPC call, and then you're golden. (See this answer for more info)
Otherwise, try to do things authoritatively, by spawning them on the server (so they are always owned by the server) and manipulate them from the server.
Answer by Leepo 1 · Nov 18, 2010 at 01:59 PM
Yeah allocating id's yourself gives you a bit more control than using Instantiate so you should probably go for the manual allocating. You do indeed to pass and assign the manually allocated viewid's on every client (and the server).
You need one scene networkview to transfer this (or a Network.Instantiated networkView).
I always have one gameobject which I call GameManager or GameSetup that has the games GUI and gameplay scripts on it. This also has a networkview which I use to set up the game and pass the allocated networkviews around with.
Answer by Dreamside · Oct 31, 2011 at 06:29 PM
i think your synch problem is not related with instantiating gameobjects.What i have done is this: Each peers loads scene but wait for server authentication.When server player loads it sends peers a int parameter that indicates time for loading.Peers receive this call and apply this equation :wait time=(parameter sent by server)-(package time stamp).all peers calculate this including server and when wait time is over they start game simultaneously.I have tested this loading approach with 5 sec network latency and each peer has different hardware for level load.When level loaded, i see that in a 1mbit connection while all players were active and average package rate was 1k/sec in whole network, there was a 20ms latency in all packages.What i mean is this:assume an object named obj receives update from a peer with a latency of 20ms.By the way in my networked game while i was testing this,there were nearly 2k gameobjects with networkview.
Answer by Dreamside · Oct 31, 2011 at 06:29 PM
As an answer to your question in EDIT, when you attach a networkview to a game object it has a unique id as you mentioned.When you use network.instantiate it calls a rpc in peers wtih a parameter which indicates related gameobjecs->networkview->networkviewid.Built in network handler gets this message and find the prefab which has same id.Then instantiates it.
Your answer
Follow this Question
Related Questions
Unet NetworkServer.Spawn() not working 5 Answers
Players Not Showing 0 Answers
Architecture for a network game- problems with events 0 Answers
How can I spawn a GameObject on all players in my multiplayer game? 0 Answers
Networking-multplayer issue 1 Answer