Custom properties issues
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:
- if (FindObjectOfType<GameplayManager>() != null)
- {
- if (!FindObjectOfType<GameplayManager>().longRoundRunning && !FindObjectOfType<GameplayManager>().shortRoundRunning)
- {
- if (transform.tag == "Enemy")
- {
- _myCustomProperties["Dead"] = true;
- PhotonNetwork.LocalPlayer.SetCustomProperties(_myCustomProperties);
- view.RPC("DestroyUs", RpcTarget.All);
- }
- }
- }
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:
- IEnumerator PlayerCountUpdate()
- {
- actualPlayerCount = PhotonNetwork.CurrentRoom.PlayerCount;
- yield return new WaitForSeconds(0.1f);
- if (PhotonNetwork.IsMasterClient)
- {
- foreach (var p in PhotonNetwork.PlayerList)
- {
- if ((bool)p.CustomProperties["Dead"] == true)
- {
- actualPlayerCount -= 1;
- }
- }
- }
- actualPlayerCountForText = actualPlayerCount;
- }
Now comes the winner finding -
Inside Player there's this function:
- public void OnGameEnd()
- {
- if (transform.tag == "Player")
- {
- _myCustomProperties["Winner"] = true;
- PhotonNetwork.LocalPlayer.SetCustomProperties(_myCustomProperties);
- }
- }
Basically means, whoever has the "Player" tag when the game ends, is the last one alive.
In GameplayManager,
This thing runs as an RPC:
- IEnumerator CloseGame()
- {
- yield return new WaitForSeconds(0.1f);
- foreach (var p in PhotonNetwork.PlayerList)
- {
- if ((bool)p.CustomProperties["Winner"])
- {
- winnerName = p.NickName;
- }
- }
- winnerText.text = winnerName;
- gameOverlay.SetActive(false);
- winnerOverlay.SetActive(true);
- yield return new WaitForSeconds(15f);
- kickEveryone = true;
- }
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
-
I can not really help debug the code but if you don't mind, I'll chime in with some general help.
Have a look at the Synchronization and State doc. I would possibly use Custom Properties for the state (who is alive, etc).
To get things going: Start minimal. Do one thing at a time. If you don't have a proper list of players, then nothing else will work. Get this working and then add stuff. If that breaks, you may want to work on it separate from the main project, to avoid parts interact negatively.
0 -
Thank you for your response.
0