Search Unity

Third Party Photon round timer not resetting when going back to lobby

Discussion in 'Multiplayer' started by Barry100, Mar 24, 2015.

  1. Barry100

    Barry100

    Joined:
    Nov 12, 2014
    Posts:
    200
    As the title says, when I use the round timer script it doesn't reset when I go back to the lobby.
    Its like the timer keeps counting when I leave the room but I can't understand how that can be if the room is closed when all players disconnect?

    And advice?
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    The timer is not a full blown solution but more of a sample how you could use the synced time in a room.
    You need to take a look at the code and disable the timer when you leave the room.
     
  3. Barry100

    Barry100

    Joined:
    Nov 12, 2014
    Posts:
    200
    ah right cool.. so how do I do that ?
     
  4. Barry100

    Barry100

    Joined:
    Nov 12, 2014
    Posts:
    200
    2 mins ill post my code
     
  5. Barry100

    Barry100

    Joined:
    Nov 12, 2014
    Posts:
    200
    the round will last for 10 minutes - 30 is just for testing just now. How do I stop or remove the timer so when the round is finished I dont go back to the game and get the same timer??


    Code (CSharp):
    1. using ExitGames.Client.Photon;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. /// <summary>
    6. /// Simple script that uses a property to sync a start time for a multiplayer game.
    7. /// </summary>
    8. /// <remarks>
    9. /// When entering a room, the first player will store the synchronized timestamp.
    10. /// You can't set the room's synchronized time in CreateRoom, because the clock on the Master Server
    11. /// and those on the Game Servers are not in sync. We use many servers and each has it's own timer.
    12. ///
    13. /// Everyone else will join the room and check the property to calculate how much time passed since start.
    14. /// You can start a new round whenever you like.
    15. ///
    16. /// Based on this, you should be able to implement a synchronized timer for turns between players.
    17. /// </remarks>
    18. public class InRoomRoundTimer : MonoBehaviour
    19. {
    20.     public int SecondsPerTurn = 30;                  // time per round/turn
    21.     public double StartTime;                        // this should could also be a private. i just like to see this in inspector
    22.     public static bool StopTimer = false;
    23.  
    24.     private bool startRoundWhenTimeIsSynced;        // used in an edge-case when we wanted to set a start time but don't know it yet.
    25.     private const string StartTimeKey = "st";       // the name of our "start time" custom property.
    26.  
    27.     public Text RoundTimer;
    28.  
    29.     private void StartRoundNow()
    30.     {
    31.         // in some cases, when you enter a room, the server time is not available immediately.
    32.         // time should be 0.0f but to make sure we detect it correctly, check for a very low value.
    33.         if (PhotonNetwork.time < 0.0001f)
    34.         {
    35.             // we can only start the round when the time is available. let's check that in Update()
    36.             startRoundWhenTimeIsSynced = true;
    37.             return;
    38.         }
    39.         startRoundWhenTimeIsSynced = false;
    40.  
    41.      
    42.  
    43.         ExitGames.Client.Photon.Hashtable startTimeProp = new Hashtable();  // only use ExitGames.Client.Photon.Hashtable for Photon
    44.         startTimeProp[StartTimeKey] = PhotonNetwork.time;
    45.         PhotonNetwork.room.SetCustomProperties(startTimeProp);              // implement OnPhotonCustomRoomPropertiesChanged(Hashtable propertiesThatChanged) to get this change everywhere
    46.     }
    47.  
    48.  
    49.     /// <summary>Called by PUN when this client entered a room (no matter if joined or created).</summary>
    50.     public void OnJoinedRoom()
    51.     {
    52.         if (PhotonNetwork.isMasterClient)
    53.         {
    54.             this.StartRoundNow();
    55.         }
    56.         else
    57.         {
    58.             // as the creator of the room sets the start time after entering the room, we may enter a room that has no timer started yet
    59.             Debug.Log("StartTime already set: " + PhotonNetwork.room.customProperties.ContainsKey(StartTimeKey));
    60.         }
    61.     }
    62.  
    63.     /// <summary>Called by PUN when new properties for the room were set (by any client in the room).</summary>
    64.     public void OnPhotonCustomRoomPropertiesChanged(Hashtable propertiesThatChanged)
    65.     {
    66.         if (propertiesThatChanged.ContainsKey(StartTimeKey))
    67.         {
    68.             StartTime = (double)propertiesThatChanged[StartTimeKey];
    69.         }
    70.     }
    71.  
    72.     /// <remarks>
    73.     /// In theory, the client which created the room might crash/close before it sets the start time.
    74.     /// Just to make extremely sure this never happens, a new masterClient will check if it has to
    75.     /// start a new round.
    76.     /// </remarks>
    77.     public void OnMasterClientSwitched(PhotonPlayer newMasterClient)
    78.     {
    79.         if (!PhotonNetwork.room.customProperties.ContainsKey(StartTimeKey))
    80.         {
    81.             Debug.Log("The new master starts a new round, cause we didn't start yet.");
    82.             this.StartRoundNow();
    83.         }
    84.     }
    85.  
    86.  
    87.     void Update()
    88.     {
    89.  
    90.         RunTimer();
    91.         if (startRoundWhenTimeIsSynced)
    92.         {
    93.             this.StartRoundNow();   // the "time is known" check is done inside the method.
    94.         }
    95.     }
    96.  
    97.     public void RunTimer()
    98.     {
    99.         // print (PhotonNetwork.time);
    100.         if (StopTimer == false) {
    101.         double elapsedTime = (PhotonNetwork.time - StartTime);
    102.         double remainingTime = SecondsPerTurn - (elapsedTime % SecondsPerTurn);
    103.    
    104.         double minutes = (int)remainingTime/60;
    105.         double seconds = (int)remainingTime%60;
    106.        
    107.         if (remainingTime <0.1) {
    108.                 remainingTime = 0;
    109.                 StopTimer = true;
    110.                 print ("end of time");
    111.                 RoundTimer.GetComponent<Text>().text= "Game Over";
    112.  
    113.      
    114.         } else {
    115.  
    116.  
    117.         RoundTimer.GetComponent<Text>().text = "Remaining Time: " + minutes.ToString() + ":" +seconds.ToString();
    118.             }}
    119.     }
    120. }
    121.  
     
  6. janeshvarmishra

    janeshvarmishra

    Joined:
    Aug 24, 2015
    Posts:
    5
    hello did you get the solution of it....please i have also same problem..sop lease share with me how you have solved it.