Custom properties issues

Options

Please bare with me because this is going to get a little long, and I REALLY need help, I have tried everything.


I am trying to implement a player list to only include alive players, and announce the winner when only 1 alive player remains.


I have a Player class and a GameplayManager class.

Here (inside Player class) I basically check if either the short round or long round aren't running, which means the round has ended, to check who should die and who should remain alive:


  1. if (FindObjectOfType<GameplayManager>() != null)
  2. {
  3. if (!FindObjectOfType<GameplayManager>().longRoundRunning && !FindObjectOfType<GameplayManager>().shortRoundRunning)
  4. {
  5. if (transform.tag == "Enemy")
  6. {
  7. _myCustomProperties["Dead"] = true;
  8. PhotonNetwork.LocalPlayer.SetCustomProperties(_myCustomProperties);
  9. view.RPC("DestroyUs", RpcTarget.All);
  10. }
  11. }
  12. }

Inside DestroyUs I set the tag to "Spectator" (This is important for later)

In GameplayManager, this is how I calculate the player count (inside an IEnumerator to let all classes update and then run this). This runs everytime a player leaves/joins or when a round has ended.

This thing runs as an RPC:


  1. IEnumerator PlayerCountUpdate()
  2. {
  3. actualPlayerCount = PhotonNetwork.CurrentRoom.PlayerCount;
  4. yield return new WaitForSeconds(0.1f);
  5. if (PhotonNetwork.IsMasterClient)
  6. {
  7. foreach (var p in PhotonNetwork.PlayerList)
  8. {
  9. if ((bool)p.CustomProperties["Dead"] == true)
  10. {
  11. actualPlayerCount -= 1;
  12. }
  13. }
  14. }
  15. actualPlayerCountForText = actualPlayerCount;
  16. }


Now comes the winner finding -

Inside Player there's this function:

  1. public void OnGameEnd()
  2. {
  3. if (transform.tag == "Player")
  4. {
  5. _myCustomProperties["Winner"] = true;
  6. PhotonNetwork.LocalPlayer.SetCustomProperties(_myCustomProperties);
  7. }
  8. }

Basically means, whoever has the "Player" tag when the game ends, is the last one alive.


In GameplayManager,

This thing runs as an RPC:


  1. IEnumerator CloseGame()
  2. {
  3. yield return new WaitForSeconds(0.1f);
  4. foreach (var p in PhotonNetwork.PlayerList)
  5. {
  6. if ((bool)p.CustomProperties["Winner"])
  7. {
  8. winnerName = p.NickName;
  9. }
  10. }
  11.  
  12. winnerText.text = winnerName;
  13. gameOverlay.SetActive(false);
  14. winnerOverlay.SetActive(true);
  15. yield return new WaitForSeconds(15f);
  16. kickEveryone = true;
  17. }


Now, why am I posting this?

Because none of this works properly.



First of all, player count is unreliable and inaccurate most of the time.

It shows different values for different players, updates wrong and such.


When it does run correctly, the Winner name display is always wrong. Always shows some random name without taking into consideration who won and who hasn't.


I am completely lost. I tried looking at everything online before posting here, I really need answers. Please help me.


Thank you.

Answers