Custom property is unseen for who join room later

Options
Hi everyone,

I'm having this issue:
I'm created simple custom property for keeping score of team,but when someone joins game later it still looks 0 for new player.

Here is my game manager script:

public int TeamScore;

private void Awake()
{
m_playerCustomProperties.Add("TeamScore", TeamScore);
PhotonNetwork.player.SetCustomProperties(m_playerCustomProperties);
}


void LateUpdate ()
{
TeamScore = (int)PhotonNetwork.player.CustomProperties["TeamScore"];

}

[PunRPC]
void AddScores()
{
int curPointTeam = (int)(PhotonNetwork.player.CustomProperties["TeamScore"]);
PhotonNetwork.player.SetCustomProperties(new ExitGames.Client.Photon.Hashtable() { { "TeamScore", curPointTeam + 10 } });
}

I'm calling "AddScores" method from each player's health script when they die like this.

This is my health script:

if (health <= 0)
{
if (photonView.isMine)
{
gameManager.photonView.RPC("AddScores", PhotonTargets.All);
}
health = 100;
}

Comments

  • OneManArmy
    OneManArmy ✭✭✭
    edited June 2019
    Options
    Save team score is room properties, not player properties.
    PhotonNetwork.room.SetCustomProperties(...);
  • 3dasd
    Options
    I changed everything into PhotonNetwork.room.SetCustomProperties but I'm getting null reference exception this line PhotonNetwork.room.SetCustomProperties(m_playerCustomProperties); in Awake() method.
  • OneManArmy
    OneManArmy ✭✭✭
    edited June 2019
    Options
    It's not enough to replace player, with room.
    If you have multiple teams then add props when you create room, or in Awake.

    For example:
    	Hashtable teamScores = new Hashtable();
    	teamScores.Add("BlueTeamScore", (int)0);
    	teamScores.Add("RedTeamScore", (int)0);
    	PhotonNetwork.room.SetCustomProperties(teamScores, null, false);
    Then you can create method to save team score:
    void AddScore(int team, int score){
    	if(team < 1 || team > 2 || score == 0) return;
    	
    	int currentScore = 0;
    	if(team == 1) currentScore = (int)(PhotonNetwork.room.CustomProperties["BlueTeamScore"]);
    	else if(team == 2) currentScore = (int)(PhotonNetwork.room.CustomProperties["RedTeamScore"]);
    	
    	int finalScore = score + currentScore;
    
    	Hashtable updateScore = new Hashtable();
    	if(team == 1) updateScore.Add("BlueTeamScore", (int)finalScore);
    	else if(team == 2) updateScore.Add("RedTeamScore", (int)finalScore);
    	PhotonNetwork.room.SetCustomProperties(updateScore, null, false);
    }
    Now when you want to set blue team score, you can simply:
    AddScore(1, 10);
    and for red ream:
    AddScore(2, 10);


    P.S. Also add:
    using Hashtable = ExitGames.Client.Photon.Hashtable;

    And read about OnPhotonCustomRoomPropertiesChanged().
  • 3dasd
    Options
    Sorry for flooding, but im getting "Cannot convert from 'System.collections.hashtable to 'ExitGames.Client.Photon.Hashtable' " error at first arguments of this lines:

    1-)
    PhotonNetwork.room.SetCustomProperties(teamScores, null, false);

    2-)
    PhotonNetwork.room.SetCustomProperties(updateScore, null, false);

    Also i'm not using pun version 2.
  • OneManArmy
    OneManArmy ✭✭✭
    Options
    Add on top:
    using Hashtable = ExitGames.Client.Photon.Hashtable;
  • 3dasd
    Options
    Sir,I'm still getting null reference exception :(

    PhotonNetwork.room.SetCustomProperties(teamScores, null, false);
  • OneManArmy
    OneManArmy ✭✭✭
    Options
    post here whole script
  • 3dasd
    Options
    This Is My Game Manager Script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using Hashtable = ExitGames.Client.Photon.Hashtable;

    public class GameManager : Photon.MonoBehaviour
    {


    public Text Team1Text;
    public Text Team2Text;

    private ExitGames.Client.Photon.Hashtable m_playerCustomProperties = new ExitGames.Client.Photon.Hashtable();


    private void Awake()
    {
    Hashtable teamScores = new Hashtable();
    teamScores.Add("BlueTeamScore", (int)0);
    teamScores.Add("RedTeamScore", (int)0);
    PhotonNetwork.room.SetCustomProperties(teamScores, null, false);

    UpdateScores();

    }


    public void UpdateScoreTexts()
    {
    int blueTeamScore = 0;
    blueTeamScore = (int)(PhotonNetwork.room.CustomProperties["BlueTeamScore"]);

    Team1Text.text = "Blue : " + blueTeamScore;

    int redTeamScore = 0;
    redTeamScore = (int)(PhotonNetwork.room.CustomProperties["RedTeamScore"]);

    redTeamScore .text = "Red : " + redTeamScore;
    }

    [PunRPC]
    void AddScore(int team, int score)
    {
    if (team < 1 || team > 2 || score == 0) return;

    int currentScore = 0;
    if (team == 1) currentScore = (int)(PhotonNetwork.room.CustomProperties["BlueTeamScore"]);
    else if (team == 2) currentScore = (int)(PhotonNetwork.room.CustomProperties["RedTeamScore"]);

    int finalScore = score + currentScore;

    Hashtable updateScore = new Hashtable();
    if (team == 1) updateScore.Add("BlueTeamScore", (int)finalScore);
    else if (team == 2) updateScore.Add("RedTeamScore", (int)finalScore);
    PhotonNetwork.room.SetCustomProperties(updateScore, null, false);

    UpdateScoreTexts();
    }

    }




    This is My Health Script
    [PunRPC]
    public void TestDmg(int damage, string shootername)
    {
    health -= damage;

    if (health <= 0)
    {
    if (photonView.isMine)
    {
    gameManager.photonView.RPC("AddScore", PhotonTargets.All,1,10);
    }
    health = 100;
    }

    }
  • OneManArmy
    OneManArmy ✭✭✭
    Options
    why are you sending RPC?
  • 3dasd
    Options
    Because every clients should call this method am i wrong?
  • OneManArmy
    OneManArmy ✭✭✭
    edited June 2019
    Options
    * player from blue team dies: gameManager.AddScore(2, score);
    * player from red team dies: gameManager.AddScore(1, score);
  • 3dasd
    Options
    I'm getting error when void Awake(); Initializing even game is not starting.
  • OneManArmy
    OneManArmy ✭✭✭
    edited June 2019
    Options
    add "BlueTeamScore" and "RedTeamScore" in room props when you create room and remove Awake().
  • 3dasd
    Options
    I already tried still same error :(

    private void Awake()
    {
    Hashtable teamScores = new Hashtable();
    teamScores.Add("BlueTeamScore", (int)0);
    teamScores.Add("RedTeamScore", (int)0);

    Exactly this line gives me null reference exception
    PhotonNetwork.room.SetCustomProperties(teamScores, null, false);

    }

    should i try to switch pun2 ?
  • OneManArmy
    OneManArmy ✭✭✭
    edited June 2019
    Options
    how can you get "null reference exception" when Awake is removed? So, you didn't removed Awake...
    Anyway i have to go... maybe someone else will continue from here.

    P.S.
    https://armedunity.com/files/file/215-pun-classic-starter-kit/
    or
    https://armedunity.com/files/file/219-pun-2-starter-kit/

    It's clean base, where you can test different things.
    Try to add teams/team scores in this project.
  • 3dasd
    Options
    Thanks for your answers.