Best way implementing a timer

Hey all, I'm trying to find the simplest way to implement a round timer, I know that the asteroids demo has a example of a countdown timer, but I cant get my head around it. Any assistance would be greatly appreciated, thanks!

Answers

  • Meep
    Meep ✭✭✭

    Store server timestamp in the game properties with the number of milliseconds your timer should run for added onto it. Then on other clients they can get the absolute value from their own server timestamp and the one in the game properties.

  • Im a bit confused on what I'm doing with the server timestamp, am I storing it in the game properties when the game is created, or do I add it when the game is scene loaded, and its about to start, what I'm doing right now is I'm just setting the game duration when the room is created, thanks!

  • Also this is what I've got so far

        void Awake()
        {
            startTime = (int)PhotonNetwork.CurrentRoom.CustomProperties[PropertiesKey.TimeRoomKey];
            GameTime.text = TimeSpan.FromSeconds(startTime).ToString(@"mm\:ss");
        }
    
    
        void Update()
        {
            Initialize();
        }
    
    
        public void Initialize()
        {
            startTime = currentTime;
            //currentTime -= 1; I don't know how to make this number go down every second, I know that PhotonNetwork.Time and Servertimestamp might be needed but not really clear how to
            GameTime.text = TimeSpan.FromSeconds(currentTime).ToString(@"mm\:ss");
    
    
            StateCheck();
        }
    
    
        public void OnTimerRunning()
        {
            isTimerRunning = true;
        }
    
    
        private void StateCheck()
        {
            if(state == MatchState.Finished)
            {
                OnTimerEnds();
            }
        }
    
    
        public void OnTimerEnds()
        {
            isTimerRunning = false;
            Debug.Log("Game Has Ended");
            currentTime = 0;
            GameTime.text = "00:00";
    
    
            if (PhotonNetwork.IsMasterClient)
            {
                EndGame();
            }
        }  
    
    
        private IEnumerator EndGame()
        {
            PhotonNetwork.DestroyAll();
            mapCam.SetActive(true);
    
    
            yield return new WaitForSeconds(5f);
            PhotonNetwork.LeaveRoom();
            PhotonNetwork.LoadLevel("Lobby");
        }
    
  • Figured it out, thanks!