Networking problem

Hi there!

I’m pretty new to Unity, and i wanted to setup a network connection. There are two (relevant) objects in my game: one linked to a camera and a Character Motor, and one that is just a cylinder.
In each instance of the game, the Player should control the “Ego” object and see the Cylinder at the other player’s position. I tried it with this code:

function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo)

{
if (!alone)
{

var PosCopy : Vector3;
if (stream.isWriting)
{
	PosCopy = myEgo.position;
	stream.Serialize(PosCopy);
}
else
{
	stream.Serialize(PosCopy);
	otherPlayer.position = PosCopy;
}

}
}

Later i realized that the host’s Stream is only writing and the client’s stream only reading.
My question: Is there any way to make the Object send and receive data? If not, what are your suggestions?

Thanks a lot!

NetworkViews act as a kind of “data-tunnel”. The different NetworkViews are “connected” with the NetworkViewID. A NetworkViewID can only have one owner. The client that allocated the ID is the owner and he’s the only one that is able to send data. Every client is able to allocate a NetworkViewID with Network.AllocateViewID but you have to tell all others this ID so they can assign the ID to the right NetworkView.

When you use Network.Instantiate this process happens behind the scenes. The client that calls this method gets the owner and the object is instantiated on all other clients as well and the ViewID get transfered automatically. I’m not a friend of Network.Instantiate because you have almost no control over the buffered RPC that is performing the instantiate on the clients.

In your case I would use one globally NetworkView (one that is in the scene and have a scene-NetworkViewID) and a manager script that handles the communication. Use an RPC call to tell the other side what’s your NetworkViewID.

var cylinderObject : NetworkView; // assign the cylinder object here
var localPlayer : NetworkView;    // assign the player object here

function OnConnectedToServer()
{
    // client is connected to the server, generates an ID and send it to the server.
    var viewID = Network.AllocateViewID();
    localPlayer.viewID = viewID;
    networkView.RPC("SetViewID", RPCMode.Others, viewID);
}

function OnPlayerConnected (player : NetworkPlayer)
{
    // This is called on the server when the client connects. The server also generates an ID and send it to the client.
    var viewID = Network.AllocateViewID();
    localPlayer.viewID = viewID;
    networkView.RPC("SetViewID", RPCMode.Others, viewID);
}

@RPC
function SetViewID (viewID : NetworkViewID)
{
    // This is the RPC function that receives the ID and assign it to the cylinder's NetworkView
    cylinderObject.viewID = viewID;
}

This script should be on a scene object with a NetworkView. Just assign the two objects to the variables and it should work ;). But keep in mind that will only work for two players (one server, one client).

Oh and i haven’t tested it yet so no guaranty.

Thank you very much! It works perfectly.

The player and cylinder objects are cross-linked over the network :wink:

Exactly how I wanted it :slight_smile: