Creating Day/Night cycle for multiplayer in Unreal 5 - Sync Time Logic?

Hello everyone :slight_smile:

I am trying to figure out how to get a working day / night cycle that is “synced” (at least as close as can be) for a multiplayer co-op game.

My problem is this: I have a host & join session setup using listen server but I am unable to figure out how to get it to sync. Every time I start it up without the join session, i.e. spawn server and client together I can see that it works and is replicated. However, when I use the join session option the cycle is always out of sync.

For the past few days I have been scouring through YouTube, Google, Udemy, Unreal Learning etc. in order to figure out a way to make it work. So far, I have seen the same ■■■■ tutorial being recreated an ungodly amount of times but it only works for local MP or SP games.


This is my latest attempt, albeit a weak one where I can recreate it outside of using the level blueprint but I am sorely lacking a time logic and a way to sync between server and incoming client.

Does anyone know where I can find a tutorial or have an example to share? As I want the game to utilize Solar Power, it is an integral piece of the game that at least the Cycle works. The rest I can fake using Interfaces and animations :face_with_peeking_eye:

The easiest way to do this is to put a variable on the GameState for what the day/night value is, and make it replicated. Update it on the server, read it on the client.
Make it a float, valued 0 … 1 for “night … day”
In your tick, read this value, and calculate (rather than increment) the position of the sun based on the value.

You might want to only update it seldom, and to smoothly blend rather than jump on the client when you discover it’s changed.
So, each tick, get the 0 … 1 night/day value, and translate that to a position the sun should be. Then make the sun be there.

1 Like

Personally I’d snap the sun/post processes etc just prior to possession.

On level load → pre-possession → update day/night cycle to current → possess.

I will start with saying that I am not an expert; however, I claim I have day/night working in multiplayer.

I was able to get this to work by having my sun, stars and moon rotations controlled in the game mode.

The level BP gets these positions from the game mode, sets level BP replicated variables and multicasts set the rotation of sun, starts and moon for the server and the clients.

Are you using casting to get the sun from Game Mode to Level BP?

If I understand correctly, you mean a default values for night or day and then have it move between them?

Ex: On 0, set rotation X and then 1 set rotation -X?

Maybe. The sun, moon and stars are actors in the level. The Game mode is always is incrementing 3 floats which are the angles for the sun, moon and stars. The level BP periodically reads these floats by calling “Get Game Mode” followed by a cast to my gamemode BP (FirstPersonGameMode). I feed those angles into “SetActorRotation” for the sun, moon and stars.

My additional point is: Don’t think of it as “increment,” think of it as “calculate correct position based on time of day.”
You should be able to plug in a time of day value, and immediately know where the sun, moon, and stars are, without having to run the simulation a thousand times to “step” to that point.
(If you’re fancy, you also need time-of-year in addition to time-of-day, but 99% of the time, that’s overkill :smiley: )

2 Likes

So uh, update time.

I either solved it by accident or I solved it by creating bad code.

I listened to your input and took it into consideration, testing some ideas and thought of a tutorial that I found some way back by C:\InsertNameHere:

I followed this one as it basically has everything I wanted (and a bit more), fudging around and testing it on GameState first but to no avail, it just would not work even when I tried casting and updating (most likely doing it wrong as I am fairly new to this).

So I followed along part 1 and just did what VespineAuto007 suggested and moved everything to GameMode instead, replicating my custom SunSky BP, making sure that replicate was placed on both Sun and Moon Directional Lighting and placing some calls in Level BP.

It worked, everything updates simultaneously on both server and client.

Except that it updates on Server and Client when I removed all logic Level BP, which if I understand the general idea of Multiplayer logic should not be possible. Considering that GameMode stores logic for server only so client should just see a light fixed in the sky.

The only reason why I think it works is because Actor & Directional Lights are set to replicate, and while I am happy, I also feel very confused.

Code that I entered into GameMode

Replicate option True in Actor BP_SunSky

image

Component Sun replication True, Moon in this BP setup is a child of Sun but still has the same option as True

image

This will eventually saturate the network.

You want to do an initialization update on the client when the map loads for them. This will get the current state of the day/night cycle on the server.

Beyond that maybe setup a timer that executes once every 10-30 seconds that “Requests” the current state.

As long as the cycle is persistent you should only have to request current state a few times. You’ll always be behind the servers current by ping time + processing.

2 Likes