Displaying Game Over Win/Lose to players

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Displaying Game Over Win/Lose to players

jgonzosan
2018-09-26 21:01:30

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().GameOver(true);  
                Debug.Log("Player " + player.ID + " won");

                if (player.GetScore() < pointsToWin)  
                {

                    GameManager_Basic.Instance.localPlayer.GetComponentInChildren().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

[Deleted User]
2018-09-27 08:59:37

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.

jgonzosan
2018-09-27 12:53:04

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().GameOver(false);  
             

                if (player.GetScore() < pointsToWin)  
                {  
                    GameManager.Instance.livePlayers[i].GetComponentInChildren().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.

[Deleted User]
2018-09-27 14:12:06

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.

jgonzosan
2018-09-27 14:16:19

Thanks, I'll give it a try!

Yoethan64
2021-04-07 06:46:05

But how do I display the winner's name?

Tobias
2021-04-07 15:38:48

Use player.NickName. Provided everyone sets a NickName, it gets synchronized.

Back to top