Loop + Networking question (Buffered RPC)

Hi Unity,

I am stuck and need some help please :slight_smile:

I am trying to display all player’s names that are in a matchmaking room together. I am using RPC’s to do this.

So I have a List<> of strings called playerNames. When a player joins the game, the send an RPC called "ReceiveJoinedPlayerNames(string pName). This is sent in the start() method.

It is sent as others.buffered.

Now that method looks like this:

[RPC]
void ReceiveJoinedPlayerNames(string pName)
{
	playerNames.Add(pName);
}

So essentially, it adds that player’s name to the player list. The player’s own name is also added to that list in the start method.

The first person to join the game sees both his name and the player that joined, the second player only sees his own name.

Its as if he never receives the buffered RPC that was sent. I am not sure why he doesnt receive it, any thoughts? Here is pretty much the full code:

// Use this for initialization
	void Start () 
	{

//send everyone your name
		photonView.RPC("ReceiveJoinedPlayerNames",PhotonTargets.OthersBuffered, PhotonNetwork.playerName);
		
		//add your name to your own player list
		playerNames.Add(PhotonNetwork.playerName);
		
		
	}

	void Update () 
	{
		UpdateTimer();
	
		UpdatePlayerList();
	}
	
	void UpdatePlayerList()
	{
		for(int cnt = 0; cnt < playersText.Length; cnt++)
		{
			//either show a player or open
			if(cnt < playerNames.Count) // if on first past there is a player in here
			{
				//show their name
				playersText[cnt].GetComponent<UILabel>().text = playerNames[cnt];
			}
		}
		
		
	}
	
	[RPC]
	void ReceiveJoinedPlayerNames(string pName)
	{
		playerNames.Add(pName);
	}

Noooooooooo! Stop! You’re reinventing the wheel :stuck_out_tongue:

Photon (and even out-of-the-box unity networking) does this for you. Because you tagged your question with photon cloud, I will show you how to do so using PUN.

You can access all the players in the room via the otherPlayers field on PhotonNetowork and can use it like so:

foreach(var player in PhotonNetwork.otherPlayers) {
   // do something with player
}

Further, say you wanted all the players, including yourself. They got you covered there too:

foreach(var player in PhotonNetwork.playerList) {
       // do something with player
}

Much easier than trying to do that by hand.