NetworkPlayer ID of the player sending RPC to server.

I am sure this is pretty simple, and straight forward, but I cant seem to figure out how to do this.

I am trying to make sure that a player/client choose a team after he connected to the server. So the client have a choice to click on button 1 or 2, once this is done, client send a RPCMode.Server, telling it(the server) to spawn the units for the client and at the same time attach the team number to the units.

But for some reason, I cant get the NetworkPlayer ID of the client/player that sends the RPC to the server, and thus, I cant tell the client that he is allowed to move the units. Is there a simple way to do this?

Every RPC has an extra “last” parameter of type NetworkMessageInfo. This structure has a field called sender. So to check who sent an RPC you can simply do something like this…

[RPC]
public void SomeRPCOnTheServer(int aParameter, NetworkMessageInfo info)
{
// do stuff with the info.sender
}

See http://docs.unity3d.com/Documentation/Components/net-RPCDetails.html for more info.

P.S. I strongly encourage anyone doing networking in Unity to use uLink by www.muchdifferent.com. I’m not affiliated with them. But their product rocks the casbah.

Ok, so I found a solution… however, it was not “pretty simple” or straight forward, so if anyone happens to know a better way for dooing this, please let me know.

Here is my current solution. Let me know if it is not complete, and does not make sense, it is ripped out from my code, that is a “bit” too complex to post here for a simple answer.

I start out on the server.

public NetworkView ncoNetworkView; //The NetworkControlObject, wich is activef on the client.

public void OnPlayerConnected (NetworkPlayer newPlayer)
{
   ncoNetworkView.RPC("GetNetworkPlayer", RPCMode.AllBuffered, newPlayer);
}

Then in the NetworkControlObjectScript (attached to the networkview) I do the following

//This is to make sure that we remember the Networkplayer that is used on the server.

	[RPC]
	public void GetNetworkPlayer (NetworkPlayer player)
	{
		owner = player;
		if (player == Network.player) {
			guiOn = true;
		}
	}

	void OnGUI ()
	{
		if (guiOn) {
			GUILayout.BeginArea (new Rect (Screen.width / 2 - 125, Screen.height - 85, 250, 80));
			GUILayout.Box ("This is the size of the unitarray: " + unitList.Count);
			if (GUILayout.Button ("Join Team 1")) {
				spawnView.RPC ("spawnMethod", RPCMode.Server, 1, owner);
				guiOn = false;
    
			}
			if (GUILayout.Button ("Join Team 2")) {
				spawnView.RPC ("spawnMethod", RPCMode.Server, 2, owner);
    				guiOn = false;
			}
			GUILayout.EndArea ();
		}
	}

Back at the server again, I initiate the unit, and set the networkplayer to be owner, making sure that the player has control over the units that were just spawned. (And this is cut down quite a bit, since I have more things going on when spawning units, this is not interesting for the current example.)

	[RPC]
	public void spawnMethod(int teamNumber, NetworkPlayer clientPlayer)
	{
		Spawnplayer (clientPlayer, teamNumber);
	}

	public void Spawnplayer (NetworkPlayer newPlayer, int team)
	{
int playerNumber = int.Parse (newPlayer.ToString ());

//This spawnins units for the player.
		for (int i = 1; i <= numberOfUnits; i++) {
			GameObject tempUnit = (GameObject)Network.Instantiate (units, this.transform.position * (i * 0.1f), Quaternion.identity, playerNumber);
			unitList.Add (tempUnit);
			Transform modelPart = tempUnit.transform.root.Find ("Model");
			NetworkView newUnitsNetworkview = modelPart.networkView;
			newUnitsNetworkview.RPC ("SetPlayer", RPCMode.AllBuffered, newPlayer);
		}
	}

I think you’d be better off using NetworkView.owner as a parameter. As such, your CLIENT side call should look as:

networkView.RPC ("myserverfunction",RPCMode.Server, networkView.owner)

it’s quite possible that this can save you the calls back.