Hello,
Being a Unity developer for 4 years and this is my first time posting something - hurrah! -
I have no idea of Networking Programming, I just started reading about what Unity can do so here’s my question:
I have already made a test game with many small objects that I need to see in both server and client updating. Because I
don’t use prefabs and because I want to have more control over what’s happening I don’t want to use Network.Instantiate rather
add a NetworkView and work like that. I wonder if I parent to all those small objects and add the NetworkView to the parent instead of each individual small object will I be able to see the update to both client and server?
From what I understand RPCs are individual calls with serialized information but I just want to see some object’s position update constantly so I guessed NetworkStateSynchronization.Unreliable is the best way to go but do I need any RPC to trigger that?
My code is:
m_Parent = new GameObject( "Parent" );
NetworkView nw = m_Parent.AddComponent<NetworkView>();
nw.group = 0;
nw.observed = m_Parent.transform;
nw.stateSynchronization = NetworkStateSynchronization.Unreliable;
nw.viewID = Network.AllocateViewID();
if( Network.connections.Length > 0 )
{
m_Parent.transform.position = Helper.GetVectorForPct( 0.5f, 0.0f ); // position the seond set of objects a bit to the right
}
I always get:
View ID AllocatedID: 0 not found during lookup. Strange behaviour may occur
and
Received state update for view id’ AllocatedID: 150’ but the NetworkView doesn’t exist
EDIT: I do have a Master Server Setup and it’s working fine with Network Instantiated objects which I can move around but since I don’t really understand how NetworkViews work in more depth I want some clarification, thanks 
From my humble experience I assume that simply transmitting an objects transform.position values (without any more sophisticated logic, like synchronizing custom script variables etc.) should work by simply relying on the NetworkView’s stateSynchronization.
What I don’t understand in your code above: Why do you set all the networkView’s properties by code instead of just defining them in the inspector window? Is this because you want to prgrammatically change them during runtime?
Yes and also the design of the project is solid, if I want something changed I won’t have to go around and look in the editor rather I will see the code’s logic, for bigger projects it’s a life saver
Also, why do I get these errors, and will setting this NetworkView on the parent work for the children as well?
I bumped into these similar errors when I have objects (with attached NetworkView) loaded when switching level. With my limited exposure in this area and without knowing your context, I would suggest you check out the loading phases. What I had before:
Server started - connect and register to master server - switch to a real level and wait.
Client login to master server - select and connected to the server - switch to the real level.
During these phases, if I forget to stop the message pumps and properly resume the message pumps, I will run into missing Allocation ID here and there. Just my 2 cents.
I think it’s more important for me to get the same functionality as Network.Instantiate without using it like that but the direct equivalent with setting up the NetworkView
AFAIK network synchronization is NOT automatically applied to children (anybody, correct me if I am wrong!).
However, you can access the parent’s networkView in scripts, of course, i.e. instead if checking network status like this:
if (networkView.isMine) {
…
}
you can do (in scripts attached to children) …
if (root.networkView.isMine) {
…
}
(though I assume that you already knew that, as you seem to be an experienced Unity coder.)
This doesn’t provide automatic sync of the objects’ state, of course. But at least avoids having multiple networkViews within a parent/children hierarchy.
My problem is that my network knowledge is very limited not only inside Unity but in general I’ve done about 5 applications that are over the network and nothing crazy.
If you can show me an example on how to use RPCs without using prefabs etc, as I am creating my objects on runtime for various reasons.
For example I tried sending over a 2D array and that didn’t work ( of course I was aware of the potential issues ) but I wonder how you’d go about turning a 1 player game into a 2 player game by changing the least of the design as I just want to show a “realtime” change of the position of those objects and since they aren’t prefabs I lose the rest of the information.
My major problem is the errors I am getting as I can’t instantiate the way I want my objects and just using Network.Instantiate creates objects without information, just the position so I need somehow to send more information through RPCs
Adding Networking after you have coded the rest of the project is generally a cause for pain, as it has an affect on all aspects of design. As you;ve noticed, instantiation is one of the most affected areas.
To keep it simple, I suggest using Network.Instantiate, even if the prefab you are spawning is as simple as a transform with a networkview. At least to start, anyway.
yes
yes, or delta compressed if it does not move often.
no. NetworkView serialization and RPCs are quite different. By the way, if you want custom data serialized you do not always need to use an RPC. RPCs are best used for sporadic updates. In cases where you need constant updates , try using OnSerializeNetworkView, which is an optional method you can add to a MonoBehaviour, similar to Update().
Netoworking really adds complexity and frankly it is hard to find complete examples. I suggest starting a new, simple project before diving into adding networking to an existing project.
see also the RPC docs, and let us know if somethign there doesn’t make sense
http://docs.unity3d.com/Documentation/Components/net-RPCDetails.html
Thanks pakfront, yeah I guessed it’s going to be a problem. Basically I am using a bejeweled game I did which is super simple but unfortunately has some logic behind it with all those items floating around.
Yes, procedural generation is somewhat difficult, as the thinking behind unity networking assumes most people are using prefabs and levels.
For randomly created levels, I find that having a GameManager singleton object in my scene is really helpful. You can use it to bootstrap the rest of your scene. Assuming your procedural scene can be recreated given a single seed number, you may be able to construct a lot of your scene on server and client by having the server call an RPC on the client that passes only the seed number.
Alternately, have the server generate the jewels, something like this (coding from memory, probably not quite right):
jewel = Network.Instantiate (jewelPrefab, position, rotation, 0);
jewel.SetType("evil");
then in your Jewel Component:
[RPC]
public void SetType(string atype) {
//do some code to set color, etc based on type
// then tell cleints to update as well
if (networkView.isMine) {
networkView.RPC("SetType",RPCMode.Other, atype);
}
}
though frankly, this is where prefabs come in handy and rather than rpcing type, you just have EvilJewel and GoodJewel prefabs.
Ou! How cool is that?! I will give it a go and will let you know! Thanks a bunch!