- Home /
The question is answered, right answer was accepted
Photon Day Night Cycle Unsynced
Soooo, basically I have a survival game where there is a day night cycle, but since the start of the day is hard-coded by setting the time, when you join a server that has a different time and the default starting time, the time won't be synced when playing in a server. And I need help on how to sync it across the server. I have tried Googling it, but whenever someone tries to answer, they just give VERY unspecific answers, with no explanation on how to actually accomplish this.
You need to clarify what you want to achieve and why it fails. I tried but I don't get it: "since the start of the day is hard-coded by setting the time, when you join a server that has a different time and the default starting time, the time won't be synced when playing in a server."
What is hardcoded? Where? Server?
Built in to Photon, there is only the server timestamp. That is not a datetime value. It's just how long the server runs. This is good enough to sync stuff in a room.
If you need a datetime (5. Aug. 2015 10:16), you need to send this from the server or somewhere. You need a reference.
Plus one to the previous comment. You have to decide whether you're asking for a general explanation of client-server authority distribution architecture, or to "give you them codez".
In case of the first here is a good starter article:
Answer by 334499p · Aug 15, 2015 at 07:41 AM
You're going to want the day/night cycle to be controlled by a float variable. The basic idea of syncing the cycle is that when you are connected to a room, the master client gives you its cycle float value every frame and then you adjust your cycle according to that variable. For example night could be represented as 5 and day could be represented as 10 clamped to 0 (if it goes past 10 then it goes back to 0). So say its night time in the server that you join: When you join the master client will give you a value of 5 and then you change your cycle. So for the actual code:
Your going to need to edit your cycle script and the gameObject on which it is placed. On its gameObject add a PhotonView and then add this to the script after the Update function add:
[RPC]
void getTime(float time){
//Change your cycle according to the received time
}
In the Update() function for your day/night cycle script add:
if(PhotonNetwork.isMasterClient){
GetComponent<PhotonView>().RPC("getTime", PhotonTargets.Others, time);
}
The above RPC is received by ALL players EVERY frame so that if you change the time for yourself then EVERY player's day/night cycle is adjusted immediately. If you don't care about updating the cycle every frame then you could be the second part of the script (the rpc call) on a timer so that it only calls every 10 seconds or so. If you do that then you will need to lerp the cycle for each non-Master-Client so that it looks smooth.
Follow this Question
Related Questions
Blend two lightmaps for day/night cycle? (terrain) 0 Answers
Object rotation shifting 1 Answer
Day Night Cycle Script Not Working 3 Answers
Day/Nights cycle 2 Answers
Day Night cycle question 1 Answer