Question by
fluxhackspro · Jan 03, 2017 at 12:57 PM ·
scripting problemnetworkingphoton
Photon Network, synchronize a int over the network.
Hello im having problem with a scoreboard, when i try to synchronize an int which in this case is number of deaths, they dont really synchronize at all over the network. Here's my scoreboard script. I'd love to know what the problem is! Thanks
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Scoreboard : MonoBehaviour
{
public GameObject ScoreboardGO;
//Team stuff
public int Team1K;
public int Team2K;
//Text
public Text Team1Text;
public Text Team2Text;
//synchronization part!!
//HERE IS THE PROBLEM
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(Team1K);
}
else
{
Team1K = (int)stream.ReceiveNext();
}
}
void Update()
{
//THIS IS JUST FOR DEBUGGING
if(Input.GetKeyDown(KeyCode.C))
{
Team1K += 1;
}
//Setting scoreboard and toggeling
if(Input.GetKey(KeyCode.Tab))
{
ScoreboardGO.SetActive(true);
}
else
{
ScoreboardGO.SetActive(false);
}
//Set int to text
Team1Text.text = Team1K.ToString("F0");
Team2Text.text = Team2K.ToString("F0");
}
}
Comment
Hi,
you can use the custom room properties to store this kind of data, I guess this might be easier to use.
if (PhotonNetwork.inRoom)
{
ExitGames.Client.Photon.Hashtable ht = new ExitGames.Client.Photon.Hashtable() { { "Team1$$anonymous$$", Team1$$anonymous$$ + 1 } };
PhotonNetwork.room.SetCustomProperties(ht);
}
void OnPhotonCustomRoomPropertiesChanged(Hashtable propertiesThatChanged)
{
Team1$$anonymous$$ = (int) propertiesThatChanged["Team1$$anonymous$$"];
}
Best Answer
Answer by fluxhackspro · Jan 03, 2017 at 06:14 PM
Thanks @ChristianSimon
if (PhotonNetwork.inRoom)
{
ExitGames.Client.Photon.Hashtable ht = new ExitGames.Client.Photon.Hashtable() { { "Team1K", Team1K + 1 } };
PhotonNetwork.room.SetCustomProperties(ht);
}
void OnPhotonCustomRoomPropertiesChanged(Hashtable propertiesThatChanged)
{
Team1K = (int) propertiesThatChanged["Team1K"];
}