- Home /
How synchronizate my Timer using Photon?
Hello all! I made simple Timer script but I want use it in my multiplayer FPS game. So I need synchronizate Timer to all players see same time. Can you please help me? There is my Timer script:
using UnityEngine; using System.Collections; using UnityEngine.UI; public class Timer : MonoBehaviour { public int timeLeft = 5; public Text countdownText; // Use this for initialization void Start() { StartCoroutine("LoseTime"); } // Update is called once per frame void Update() { countdownText.text = ("Time Left = " + timeLeft); if (timeLeft <= 0) { StopCoroutine("LoseTime"); countdownText.text = "Times Up!"; } } IEnumerator LoseTime() { while (true) { yield return new WaitForSeconds(1); timeLeft--; } } }
Answer by ChristianSimon · Oct 09, 2017 at 12:07 AM
Hi,
you can use PhotonNetwork.time to create a synchronized timer. When the game begins you can add this value as a Custom Room Property and let each client calculate the timer on their own. Therefore the MasterClient can set this value by using the following code - or similar:
Hashtable ht = new Hashtable {{"StartTime", PhotonNetwork.time}};
PhotonNetwork.room.SetCustomProperties(ht);
You now have to implement a callback called OnPhotonCustomRoomPropertiesChanged. This might look like the following:
public void OnPhotonCustomRoomPropertiesChanged(Hashtable propertiesThatChanged)
{
object propsTime;
if (propertiesThatChanged.TryGetValue("StartTime", out propsTime))
{
startTime = (double) propsTime;
}
}
The variable 'startTime' is a local variable in my example, 'gameStarted' is a bool that describes what its name says - obviously. Having this value each client can calculate a timer on their own by using the following:
public void Update()
{
if (!gameStarted)
{
return;
}
// Example for a increasing timer
incTimer = PhotonNetwork.time - startTime;
// Example for a decreasing timer
double roundTime = 300.0;
decTimer = roundTime - incTimer;
}
Answer by Aqibsadiq · Dec 10, 2018 at 06:13 AM
Simplest Way to Implement Timer on Multiplayer Game Using Photon. Apply this Script on the UI with Timer
bool startTimer = false;
double timerIncrementValue;
double startTime;
[SerializeField] double timer = 20;
ExitGames.Client.Photon.Hashtable CustomeValue;
void Start()
{
if (PhotonNetwork.player.IsMasterClient)
{
CustomeValue = new ExitGames.Client.Photon.Hashtable();
startTime = PhotonNetwork.time;
startTimer = true;
CustomeValue.Add("StartTime", startTime);
PhotonNetwork.room.SetCustomProperties(CustomeValue);
}
else
{
startTime = double.Parse(PhotonNetwork.room.CustomProperties["StartTime"].ToString());
startTimer = true;
}
}
void Update()
{
if (!startTimer) return;
timerIncrementValue = PhotonNetwork.time - startTime;
if (timerIncrementValue >= timer)
{
//Timer Completed
//Do What Ever You What to Do Here
}
}
Please Comment if you are facing any Issue. Thanks. @juraj123
Just saved my life, man. Thank you a lot for this answer!
Thank you for your great work, it worked well in my script, I found that there are some APIs have changed.
For those who see this in 2020
PhotonNetwork.player is now PhotonNetwork.LocalPlayer
PhotonNetwork.room is now PhotonNetwork.CurrentRoom
link of changed APIs https://doc.photonengine.com/en-us/pun/v2/getting-started/migration-notes
Have a nice day
$$anonymous$$aybe I'm missing something, but if you have all the same countdown timers on every client, couldn't you just sync them once then let them run? Then pause via master when you need to stop them? $$anonymous$$aybe this is still inaccurate.
How would you sync this time if the host disconnects, then comes back?
I ended up making it work. $$anonymous$$y timer is actually a countdown, it works almost the same way but ins$$anonymous$$d of saving the server time to the room custom properties, it sends as a buffered RPC to all players who update their variable for server time. If the countdown has started, master sets a "clockisrunning" bool and it counts down. There are no issues because master goes to the next player if they leave and they can update the next call.
I and trying to create that timer that you developed! If you can help me will be great!
If I want the timer to countdown from 20 to 0 what change will be in the code?
Answer by juraj123 · Oct 08, 2017 at 02:21 PM
my Timer script: using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Timer : MonoBehaviour { public int timeLeft = 5; public Text countdownText;
// Use this for initialization
void Start()
{
StartCoroutine("LoseTime");
}
// Update is called once per frame
void Update()
{
countdownText.text = ("Time Left = " + timeLeft);
if (timeLeft <= 0)
{
StopCoroutine("LoseTime");
countdownText.text = "Times Up!";
}
}
IEnumerator LoseTime()
{
while (true)
{
yield return new WaitForSeconds(1);
timeLeft--;
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Photon position syncing 0 Answers
How can I synchronize Instantiated children of a GameObject over Photon? 1 Answer
Photon synchronization 1 Answer