Awaiting room/Lobby count down timer

Hi all. Someone can help me with my CountDownTimer script please?
This count down timer method I took from "RockPaperScissiors" demo. It sometimes works, sometimes not. Mostly doesnt works. Sometimes gives me huge number timer although I put 6 second. Thank you in advance.
using System;
using System.Collections;
using Photon;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;
using ExitGames.Client.Photon;

public class CountDownTimer : PunBehaviour, IPunTurnManagerCallbacks {

	public float prematchCountdown = 5.0f;
	public Text CountDownText;

	public PunTurnManager turnManager;

	void Start () 
	{
		this.turnManager.TurnDuration = 6f;
		this.turnManager.TurnManagerListener = this;
	}

	void Update () 
	{
		if (!PhotonNetwork.inRoom)
		{
			return;
		}
		if (PhotonNetwork.room.PlayerCount == ConnectionManager.Instance.MaxPlayer)
		 {
			if (this.turnManager.IsOver)
			   {
				 //return;
			   }

			if (this.CountDownText != null)
			{
				this.CountDownText.text = this.turnManager.Turn.ToString();
			}

			if (this.turnManager.Turn > 0 && this.CountDownText != null)
			   {
				this.CountDownText.text = "Match Starts In: " + (int)this.turnManager.RemainingSecondsInTurn;
				  
				if (this.turnManager.RemainingSecondsInTurn == 0)
					 {
						ConnectionManager.Instance.ToGameScene ();
					    print ("ChangeScene");
					 }
				}
		}
		if (PhotonNetwork.room.PlayerCount != ConnectionManager.Instance.MaxPlayer)
		{
			this.CountDownText.text = "";
		}
	}

	#region TurnManager Callbacks

	public void OnTurnBegins(int turn)
	{

	}

	public void OnTurnCompleted(int obj)
	{

	}

	public void OnPlayerMove(PhotonPlayer photonPlayer, int turn, object move)
	{

	}

	public void OnPlayerFinished(PhotonPlayer photonPlayer, int turn, object move)
	{

	}

	public void OnTurnTimeEnds(int obj)
	{

	}

	private void UpdateScores()
	{

	}

	#endregion

	public override void OnJoinedRoom ()
	{
		if (PhotonNetwork.room.PlayerCount == ConnectionManager.Instance.MaxPlayer)
		{
			if (this.turnManager.Turn == 0)
			{
				this.StartTurn ();
			}
			else
			{
				Debug.Log ("waiting for other players to join");
			}
		}
	}

	public override void OnPhotonPlayerConnected(PhotonPlayer newPlayer)
	{
		Debug.Log("Other player arrived");

		if (PhotonNetwork.room.PlayerCount == ConnectionManager.Instance.MaxPlayer)
		{
			if (this.turnManager.Turn == 0)
			{
				// when the room has two players, start the first turn (later on, joining players won't trigger a turn)
				//this.StartTurn();
			}
		}
	}


	#region Core Gameplay Methods
	public void StartTurn ()
	{
		if (PhotonNetwork.isMasterClient)
		{
			this.turnManager.BeginTurn ();
		}
	}
	#endregion
}

Answers

  • I have changed a little bit, now it lokks works. Anyway if someone can check
    using System;
    using System.Collections;
    using Photon;
    using UnityEngine;
    using UnityEngine.UI;
    using Random = UnityEngine.Random;
    using ExitGames.Client.Photon;
    
    public class CountDownTimer : PunBehaviour, IPunTurnManagerCallbacks {
    
    	public float prematchCountdown = 5.0f;
    	public Text CountDownText;
    
    	public PunTurnManager turnManager;
    
    	void Start () 
    	{
    		this.turnManager.TurnDuration = 6f;
    		this.turnManager.TurnManagerListener = this;
    	}
    
    	void Update () 
    	{
    		if (!PhotonNetwork.inRoom)
    		{
    			return;
    		}
    		if (PhotonNetwork.room.PlayerCount == ConnectionManager.Instance.MaxPlayer)
    		 {
    			if (this.turnManager.IsOver)
    			   {
    				 //return;
    			   }
    
    			if (this.CountDownText != null)
    			{
    				this.CountDownText.text = this.turnManager.Turn.ToString();
    			}
    
    			if (this.turnManager.Turn > 0 && this.CountDownText != null)
    			   {
    				this.CountDownText.text = "Match Starts In: " + (int)this.turnManager.RemainingSecondsInTurn;
    				  
    				if (this.turnManager.RemainingSecondsInTurn == 0)
    					 {
    						//ConnectionManager.Instance.ToGameScene ();
    					    print ("ChangeScene");
    					 }
    				}
    
    			/*if (this.turnManager.RemainingSecondsInTurn == 0)
    			{
    				ConnectionManager.Instance.ToGameScene ();
    				print ("ChangeScene");
    			}*/
    		}
    		if (PhotonNetwork.room.PlayerCount != ConnectionManager.Instance.MaxPlayer)
    		{
    			this.CountDownText.text = "";
    		}
    	}
    
    	#region TurnManager Callbacks
    
    	public void OnTurnBegins(int turn)
    	{
    
    	}
    
    	public void OnTurnCompleted(int obj)
    	{
    		this.OnEndTurn();
    	}
    
    	public void OnPlayerMove(PhotonPlayer photonPlayer, int turn, object move)
    	{
    
    	}
    
    	public void OnPlayerFinished(PhotonPlayer photonPlayer, int turn, object move)
    	{
    
    	}
    
    	public void OnTurnTimeEnds(int obj)
    	{
    			Debug.Log("OnTurnTimeEnds: Calling OnTurnCompleted");
    			OnTurnCompleted(-1);
    	}
    
    	private void UpdateScores()
    	{
    
    	}
    
    	#endregion
    
    	public override void OnJoinedRoom ()
    	{
    		if (PhotonNetwork.room.PlayerCount == ConnectionManager.Instance.MaxPlayer)
    		{
    			if (this.turnManager.Turn == 0)
    			{
    				this.StartTurn ();
    			}
    			else
    			{
    				Debug.Log ("waiting for other players to join");
    			}
    		}
    	}
    
    	public override void OnPhotonPlayerConnected(PhotonPlayer newPlayer)
    	{
    		Debug.Log("Other player arrived");
    
    		if (PhotonNetwork.room.PlayerCount == ConnectionManager.Instance.MaxPlayer)
    		{
    			if (this.turnManager.Turn == 0)
    			{
    				// when the room has two players, start the first turn (later on, joining players won't trigger a turn)
    				this.StartTurn();
    
    			}
    		}
    	}
    
    
    	#region Core Gameplay Methods
    	public void StartTurn ()
    	{
    		if (PhotonNetwork.isMasterClient)
    		{
    			this.turnManager.BeginTurn ();
    		}
    	}
    
    	public void OnEndTurn()
    	{
    		//this.StartCoroutine("ShowResultsBeginNextTurnCoroutine");
    		ConnectionManager.Instance.ToGameScene ();
    	}
    	#endregion
    }
Sign In or Register to comment.