Time Sync Problems

Options
I was working on designing a room timer. But I always run into 1 of 2 issues. The client either only broadcasts the default word timer. Or if they join after the timer has started then they will not get the current time so there timer starts from the top. In our case 30 seconds.Below is the script that I've been using, with my latest variation on how to get the timer to broadcast correctly. Any and all help would be appreciated.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class WaitingRoom : MonoBehaviour
{
private List playerNamePrefabs = new List();
//stores the information about the player
//public Text playerNameText;
//public Text playerIdText;
public GameObject playerNamePrefab;

//the minimum amount of players that can be in the game
private int minPlayers;

//private int minPlayers = PhotonNetwork.room.MaxPlayers/2;

//future expansion
//YouText will say YOU next your name in the list of names
//MasterText will say master next to the creater of the game in the list of names
//public Text YouText;
//public Text MasterText;


//public GameObject UIList;

//public GameObject PlayerItemPrefab;

//public LobbyMenuController lmc; //lobby menu controller


//Total time the waiting room wil stay open
float timeLeft = 30.0f;
public Text timer;

// Use this for initialization
void Start()
{
//the minimum amount of players that can be in the game
minPlayers = PhotonNetwork.room.MaxPlayers / 2;
PhotonView myPhotonView = gameObject.GetComponent();

//wait 3 seconds and refresh the playerlist
InvokeRepeating("RefreshPlayerList", 0.1f, 3.0f);

InvokeRepeating("Countdown", timeLeft, 1.0f);

if (PhotonNetwork.isMasterClient) // host starts the countdown
{
myPhotonView.RPC("ShowTime", PhotonTargets.AllBuffered);
}

//update the player count
//playerCount = playerList();
}

void Countdown()
{
//timing for the waiting room
if (PhotonNetwork.playerList.Length >= minPlayers)
{
// SceneManager.LoadScene("GameScene");
timeLeft -= Time.deltaTime;
//timer.text = "Time Left:" + Mathf.Round(timeLeft);
ShowTime();
}
else
{
timeLeft = 30.0f;
//timer.text = "Time Left:" + Mathf.Round(timeLeft);
ShowTime();
}
if (timeLeft < 0 && PhotonNetwork.playerList.Length >= minPlayers)
{
//join the JoinGame Room
//lmc.JoinGameCanvasOn();
timer.text = "Time is up";
//SceneManager.LoadScene("GameScene");
}
}

[PunRPC]
void ShowTime()
{
timer.text = "Time Left:" + Mathf.Round(timeLeft);
}

// Update is called once per frame
void Update()
{
if (PhotonNetwork.isMasterClient)
{
Countdown();
}
}


//getPlayer will get the users name and users ID
//public void getPlayer()
//{
// //PhotonNetwork.playerName = PlayerPrefs.GetString ("CurrUser"); //delete later
// //get the players name
// PlayerNameText.text = PhotonNetwork.playerName;
// //get the players ID
// //PlayerIdText.text = PhotonNetwork.player;

// //Find out which player in the list is you and which player is the Master
// //YouText.enabled = player.isLocal;
// //MasterText.enabled = player.isMasterClient;
//}

////playerList will go through the list of players and get their names and id
//// it will also return the length of the Player List(the number of people in the game)
// public int playerList()
//{
// for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
// {

// getPlayer(_player);

//}
// return PhotonNetwork.playerList.Length;
// }

void RefreshPlayerList()
{
if (playerNamePrefabs.Count > 0)
{
for (int i = 0; i < playerNamePrefabs.Count; i++)
{
Destroy(playerNamePrefabs[i]);
}

playerNamePrefabs.Clear();
}

for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
{
//Debug.Log(PhotonNetwork.playerList[i]);

GameObject g = Instantiate(playerNamePrefab);
g.transform.SetParent(playerNamePrefab.transform.parent);

g.GetComponent().localScale = playerNamePrefab.GetComponent().localScale;
g.GetComponent().localPosition = new Vector3(playerNamePrefab.GetComponent().localPosition.x, playerNamePrefab.GetComponent().localPosition.y - (i * 80), playerNamePrefab.GetComponent().localPosition.z);
g.transform.FindChild("Name").GetComponent().text = PhotonNetwork.playerList[i].ToString();
//g.transform.FindChild("numPlayers").GetComponent().text = PhotonNetwork.GetRoomList()[i].PlayerCount + "/" + PhotonNetwork.GetRoomList()[i].MaxPlayers;
//g.transform.FindChild("ButtonJoinGame").GetComponent().onClick.AddListener(() => { JoinRoom(g.transform.FindChild("game_name").GetComponent().text); });
g.SetActive(true);
playerNamePrefabs.Add(g);
}
}

//the back button wil
public void BackButton()
{
//leave the photn waiting room
PhotonNetwork.LeaveRoom();
PhotonNetwork.JoinLobby();
SceneManager.LoadScene("Lobby");
//lmc.JoinGameCanvasOn();
//go to the lobby room
//this will close the entire room if the master leaves the room (future extension)
//PhotonNetwork.room.open = false;
}
}

Comments

  • Hi @MafSavTeam,

    did you take a look at the InRoomRoundTimer script that comes with the PUN package? Maybe this helps you by creating your room timer. It uses the custom room properties to store the start time in order to let each client calculate the timer on their own. The script is located inside the 'Photon Unity Networking/UtilityScripts' directory.