How to display a player in a room

Options
Hi! im new to Photon and Networking. And i really need help!

Im trying to get players to appear in a room. I have it working for a single player but not multiplayer.

I have 4 gameobjects that each contain a WaitingLabel and a Roster (contains different characters (do i need to attach photon views to characters?))
I need to setactive each waitinglabel and roster when a player enters the room.

Im trying to use RPC but i dont know how. Do i need to attach a photon view to each WaitingLabel and Roster?
I dont think i should use OnPhotonSerializeView since it should only call the RPC when a player enters..right?

I also need to attach playerIDs to each player (player 1, player 2, player 3, player 4) and assign player names to each player.

Please teach me!
Thank you!

Here is my code:
[RPC]

void OnEnable()
	{
		RoomName.text = PhotonNetwork.room.name;
		playerName = PhotonNetwork.player.name;
		AssignSlotRPC();
	}

	void AssignSlotRPC()
	{
		if(!Roster1.activeInHierarchy)
		{
			playerID = 1;
			WaitingLabel1.SetActive(false);
			Roster1.SetActive(true);
		}
		else if(!Roster2.activeInHierarchy)
		{
			playerID = 2;
			WaitingLabel2.SetActive(false);
			Roster2.SetActive(true);
		}
		else if(!Roster3.activeInHierarchy)
		{
			playerID = 3;
			WaitingLabel3.SetActive(false);
			Roster3.SetActive(true);
		}
		else if(!Roster4.activeInHierarchy)
		{
			playerID = 4;
			WaitingLabel4.SetActive(false);
			Roster4.SetActive(true);
		}
	}

	public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) 
	{
		if(stream.isWriting) {
			stream.SendNext((float)timeLeft);
			stream.SendNext((string)playerName);
			stream.SendNext((int)playerID);
			Debug.Log ("sending float");
		}
		else {
			timeLeft = (float)stream.ReceiveNext();
			playerName = (string)stream.ReceiveNext();
			playerID = (int)stream.ReceiveNext();
		}
	}

Comments

  • vadim
    Options
    Attach PhotonView only to objects which require some synchronization (position or object state) between clients.
    If you need only logic synchronization, you may add empty 'control' object with PhotonView ('Scripts' object in demos) to use Photon features like RPC.
    To handle player join, implement OnJoinedRoom (on new player client) and OnPhotonPlayerConnected (on other clients) in MonoBehaviour script attached to 'control' object.
    Player ids can be assigned via PhotonNetwork.playerName or player's custom properties.
    PhotonNetwork.playerList gives you players in room with names and properties. Iterate over it after join and find free ids to assign to new player.
  • Urob_Oros
    Options
    Well i got the player's custom properties working but i still kind of dont understand how to setactive gameobjects in a network.
    Is my code above somewhat correct?
    The part i dont understand is how do i toggle a game object and make it visible to everyone, not just the client.
    If you need only logic synchronization, you may add empty 'control' object with PhotonView ('Scripts' object in demos) to use Photon features like RPC.
    To handle player join, implement OnJoinedRoom (on new player client) and OnPhotonPlayerConnected (on other clients) in MonoBehaviour script attached to 'control' object.

    And what do i put in OnJoinedRoom and OnPhotonPlayerConnected?
    Im using OnJoinedRoom to toggle the Room screen and im calling AssignSlotRPC to toggle the specific slot (1,2,3,4). But the active slots only show client side. How do i make the slots (Roster game object below) visible to everyone?
    [RPC]
    	void AssignSlotRPC()
    	{
                    playerID = Convert.ToInt32(PhotonNetwork.player.customProperties["ID"]);
    		switch(playerID)
    		{
    		case 1:
    			WaitingLabel1.SetActive(false);
    			Roster1.SetActive(true);
    			break;
    		case 2:
    			WaitingLabel2.SetActive(false);
    			Roster2.SetActive(true);
    			break;
    		case 3:
    			WaitingLabel3.SetActive(false);
    			Roster3.SetActive(true);
    			break;
    		case 4:
    			WaitingLabel4.SetActive(false);
    			Roster4.SetActive(true);
    			break;
    		}
    	}
    

    EDIT: I got it. I didnt know i had to call an RPC like this:
    GetComponent<PhotonView>().RPC("AssignSlotRPC",PhotonTargets.AllBufferedViaServer, new object[]{playerID});