- Home /
Set NetworkViewID manually
We want to create a Multiplayer-Game with multiple Game Instances. In that case that every Game Instances needs to have its own NetworkViewID I have the following question:
Is it possible to set the NeworkViewID manually per script. I think about something like that:
NetworkView.ViewID = 10;
Is this possible to do, because I don't like to use the Network.AllocateViewID() function. Ah btw I'm writing in C#.
Answer by stevethorne · Mar 06, 2014 at 03:31 PM
NetworkViewID's are the classes that store the information necessary for for a NetworkView. You can't set the number for this ViewID because Unity needs to make sure that the view is allocated and available. The issue that you think you're having is not an issue at all.
You are going to have multiple clients connecting to your server, that's what the NetworkViewID's are for! They'll work perfectly, but you need to know how to set them up properly. I'll explain the example from the link I posted in the comments to help you better understand how these work and how you can manipulate them.
public Transform cubePrefab;
void OnGUI() {
if (GUILayout.Button("SpawnBox")) {
NetworkViewID viewID = Network.AllocateViewID();
networkView.RPC("SpawnBox", RPCMode.AllBuffered, viewID, transform.position);
}
}
First is this bit of code.
There is a button displayed on the GUI that says "SpawnBox" and, when clicked, it sends a message to every connected application. Let me tear this function call apart for you so you can better understand it.
"SpawnBox" = This is the RPC function that is going to be called.
RPCMode.AllBuffered = This means that it will be called on every connected application and it's buffered. This means that if the application connects after this function was called, it will call this function because it will be in the buffer and the new application needs to catch up.
The rest of the parameters are the parameters in the function that's going to be called.
So this function is sending the viewID that was just allocated by Unity and the position that they want the box spawned at.
[RPC]
void SpawnBox(NetworkViewID viewID, Vector3 location) {
Transform clone;
clone = Instantiate(cubePrefab, location, Quaternion.identity) as Transform as Transform;
NetworkView nView;
nView = clone.GetComponent<NetworkView>();
nView.viewID = viewID;
}
This is the function that's going to be called on every application. Note that it has the [RPC] attribute set for the function; this is needed for the function to be called through the RPC function. It's spawning a cube at the location provided from the application that sent the message and then assigns the viewID that was passed in from the other application to the NetworkView on the cube prefab.
This process makes sure that all the objects that are supposed to behave as the same object, have the same viewID on every client and the server.
I hope this clears up your confusion on the subject of NetworkViewIDs!
I want to have fix ID's for my Game Instances that's why I don't want to use AllocateViewID.
How has the function to be to change the ID ?
I can't think of a reason why you would need fixed viewIDs, but what you wrote initially wont work.
Have you tried it yet? It might be that the only way you can use an ID is if it's received from AllocateViewID function. The function might do some other things to make that ID available for use. Take a look at this link if you're not sure how to use AllocateViewID properly. When you allocate the ID you can send it to every client that's running to let them know what the new one should be.
I imaging that you can use any ID that you want. But, you'll run the risk of using an ID for the wrong thing and have major issues. It's better if you use the function provided, but I don't think it's absolutely necessary.
I want to have manually ID's because I have a game with multiple game instances on the Server. So i want to use my ID's becoause I have a logic to set them.
But If I use my written function it says: You can't convert type int into UnityEngine.NetworkViewID.
I updated the answer to explain to you how the NetworkViewID's work and why you can't set them to some number that you want.
Answer by X28 · Jul 25, 2015 at 03:54 PM
In Editor : I Duplicate object that Contain networkView several Times. in each Duplication Unity Increase the ID number of ViewID. at last I Deleted the previous Object and Keep the New One. it has ID number different from 1.
this is a WAY. you can Choose another way ;)