How can i get (or create) a real time clock for my multiplayer game?

Hi there,

I'm trying to find a way to use real (shared) time in my multiplayer game. Is there any way to get this information (like hours, minutes, seconds)?

Thanks!!

Comments

  • I let here the script that i made to get the time (not multiplayer) and create different events for my game. My goal now is to make it work with multiplayer.

    CODE:
    public class EventController : MonoBehaviour
    {
        [System.Serializable]
        public class EventsInfo
        {
            public int event_id;
            public string event_name;
    
            public int eventHour;
            public int eventMin;
            public int eventSec;
    
            public bool cyclicEvent;
            public int cooldownHour;
            public int cooldownMin;
            public int cooldownSec;
    
            public bool refreshCooldown;
        }
    
        public List<EventsInfo> eventsList = new List<EventsInfo>();
    
        public Text dateText;
        public bool saveData = false;
        public bool eventRunning = false;
    
        public int savedHour;
        public int savedMin;
        public int savedSec;
    
        // Start is called before the first frame update
        void Start()
        {
            
        }
    
        // Update is called once per frame
        void Update()
        { 
            StartCoroutine(UpdateTime());
    
            EventCallerAfterTime();
    
            Debug.Log("ServerTimeStamp" + PhotonNetwork.ServerTimestamp);
        }
    
        IEnumerator UpdateTime()
        {
            while (true)
            {
                var today = System.DateTime.Now;
                dateText.text = today.ToString("dd-MM-yyyy HH:mm:ss");
                yield return new WaitForSeconds(1);
            }
        }
    
        private void EventCallerAfterTime()
        {
            //Realtime
            var today = System.DateTime.Now;
    
            //SaveTempDataTime
            if (!saveData && !eventRunning)
            {
                savedHour = today.Hour;
                savedMin = today.Minute;
                savedSec = today.Second;
                saveData = true;
    
                Debug.Log("Hour saved: " + savedHour);
                Debug.Log("Minute saved: " + savedMin);
                Debug.Log("Second saved: " + savedSec);
            }
    
            if (eventRunning)
            {
                //LOAD CURRENT EVENT.
            }
    
    
            //RECORREMOS LA LISTA DE EVENTOS PARA REALIZAR LLAMADAS DE EVENTOS
            for (int i = 0; i < eventsList.Count; i++)
            {
                if (today.Hour == eventsList[i].eventHour)
                {
                    if(today.Minute == eventsList[i].eventMin)
                    {
                        if(today.Second == eventsList[i].eventSec)
                        {
                            Debug.Log("EVENT CALL " + eventsList[i].event_name + " (" + eventsList[i].event_id + ")");
                            Event(i);
    
                            if (eventsList[i].refreshCooldown)
                            {
                                eventRunning = true; 
    
                                if (eventsList[i].cyclicEvent)
                                {
                                    if(eventsList[i].cooldownHour != 0)
                                        eventsList[i].eventHour = eventsList[i].eventHour + eventsList[i].cooldownHour;
    
                                    if (eventsList[i].eventMin == 24)
                                        eventsList[i].eventMin = 0;
                                }
                                if (eventsList[i].cyclicEvent)
                                {
                                    if (eventsList[i].cooldownMin != 0)
                                        eventsList[i].eventMin = eventsList[i].eventMin + eventsList[i].cooldownMin;
    
                                    if (eventsList[i].eventMin == 60)
                                        eventsList[i].eventMin = 0;
                                }
                                if (eventsList[i].cyclicEvent)
                                {
                                    if (eventsList[i].cooldownSec != 0)
                                        eventsList[i].eventSec = eventsList[i].eventSec + eventsList[i].cooldownSec;
    
                                    if (eventsList[i].eventSec == 60)
                                        eventsList[i].eventSec = 0;
                                }
    
                                eventsList[i].refreshCooldown = false; //WWhen the event's finished refreshCooldown turns true.
                            }
                        }
                    }
                }
            }
        }
    
        private void Event(int id)
        {
            Debug.Log("Execution of the event " + id);
    
            eventsList[id].refreshCooldown = true;
        }
    }
    
  • Hi @albertduranll,

    Thank you for choosing Photon!

    PUN2 has a UtilityScript for in room countdown timer: CountdownTimer.cs.
    It makes use of the synchronize server time on all clients: PhotonNetwork.ServerTimestamp.

    You can also use PhotonNetwork.Time directly.
  • edited July 26
    I'll try it, thank you very much!
Sign In or Register to comment.