How can i get (or create) a real time clock for my multiplayer game?
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
How can i get (or create) a real time clock for my multiplayer game?
albertduranll
2020-07-14 14:31:42
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
albertduranll
2020-07-14 14:38:03
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.
albertduranll
2020-07-26 12:28:15
I'll try it, thank you very much!
Back to top