Displaying Game Over Win/Lose to players

Currently within my game I want to have a display appear on screen when players reach a certain point limit. It works for the most part. Where I can check to see what everyone's points are at then determine when I should display a game over image that says "you won" or "you lost". The problem is that everyone receives the same message. I know this is due to it being local.

My question is how can I reference a script assigned to a specific character? Right now I have this:
 if (player.GetScore() >= pointsToWin)
            {
                GameManager_Basic.Instance.localPlayer.GetComponentInChildren<PlayerMenus>().GameOver(true);
                Debug.Log("Player " + player.ID + " won");

                if (player.GetScore() < pointsToWin)
                {

                    GameManager_Basic.Instance.localPlayer.GetComponentInChildren<PlayerMenus>().GameOver(false);
                    Debug.Log("Player " + player.ID + " lost");

                }

            }
I was adding players to a list but realized this was just being added locally so every time a networked player appeared they were added to their own local list so the method to display the game over was called locally. Is there a way to create a list of all the networked players then reference a specific script attached to them? I believe I'm on the right track I just need to do this on a networked list. Is there a better way to approach this?

Comments

  • Hi @jgonzosan,

    is this a follow-up to your other thread?

    If so, when you use PunPlayerScores, the scores are synchronized across all clients, so you don't have to store them manually. To get the local player's score, you can use PhotonNetwork.player.Getscore();. To get the score of the other clients, you can iterate through PhotonNetwork.playerList (this includes the local player) and get their score by using the GetScore() function again.
  • It's a bit different. I have the scores, I can add them and display them. I also iterate through the player list like you mentioned. The main issue is when the game is over I want one player to see "Game Over, You Win" and the other(s) to see "Game Over, You Lost". I mostly need to know how I can access other players' scripts over the network when a points threshold has been met.
    This is my entire code block for updating the points then also checking to see if any of the players have met the points to then display the GameOver state to players:

     void OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps)
        {
    
            PhotonPlayer[] pList = PhotonNetwork.playerList;
            System.Array.Sort(pList, delegate (PhotonPlayer p1, PhotonPlayer p2) { return p1.ID.CompareTo(p2.ID); }); System.Array.Reverse(pList);
    
            for (int i = 0; i < pList.Length; i++)
            {
                PhotonPlayer player = pList[i];
                scoreText[i].SetText(player.GetScore().ToString());
                Debug.Log("Player " + player.ID + " score " + player.GetScore());
    
                if (player.GetScore() >= pointsToWin)
                {
                    Debug.Log("Player " + player.ID + " has lost");
                    GameManager.Instance.livePlayers[i].GetComponentInChildren<PlayerMenus>().GameOver(false);
                 
    
                    if (player.GetScore() < pointsToWin)
                    {
                        GameManager.Instance.livePlayers[i].GetComponentInChildren<PlayerMenus>().GameOver(true);
                    }
                }
    
    
            }
        }
    I originally had it setup so that players join, then get added to a list of "live players" on the GameManager but I realized that only happened locally so it was pointless. I'm wondering if I can just do a local check for points on each player instead of having a list to iterate through. So using "GetScore" within each individual player and having them each check their own points.
  • You have different possibilities to do this:

    The first one is to use the MasterClient and let him decide when to end the game. Therefore he can iterate through each player's score and check, if at least one client has reached a certain amount of points. If not, the game can continue. However if a client has enough points and won the game, the MasterClient can raise an event, to notify each client, that the game is over. To see how raising an event works, I recommend you taking a look at the RPCs and RaiseEvent documentation page.

    The second option is to let each player check his own points individually. This can be for example done, after updating the score locally or in the OnPhotonPlayerPropertiesChanged callback (only after the local client's properties are updated). If one client has enough points to win the game, he can raise an event to notify each other client. See the link above.

    At this point, both possibilities continue with the same logic: whenever a client receives an event, his registered OnEvent handler is called. When the client receives the 'GameOver' event, he can check his score. If he has enough points to be the winner, you can display this result on his screen. If he doesn't have enough points, you can display this result on his screen as well.

    Instead of just sending the 'GameOver' event, you could also pass the name of the winner with this event. In this case you could write the name of the winner on each clients' screen. Adding the winner's score would also be possible.
  • Thanks, I'll give it a try!
  • But how do I display the winner's name?
  • Use player.NickName. Provided everyone sets a NickName, it gets synchronized.