Skip to content

UE4 – Day and Night Cycle for Multiplayer (Part 1)

Let’s create a beautiful day and night cycle for a multiplayer game in Unreal 4. Our night will be a pitch black and day will have a spectacular sky and lighting. In bonus part we’ll create a moon and lighting and to go even further on i’ll show you how to do it in C++. This system will have a full dynamic lighting so no baking and sitting and waiting for hours, you’ll have a new feature in your game before you go to bed.


Blueprint Only Project Files

https://levelparadox.gumroad.com/l/yuhsi


First we need to enable Sun Position Plugin.

Goto Settings -> Plugins and enable Sun Position Calculator.

Add Sun and Sky to your level.

Select View Options in Content Browser and turn on Show Plugin Content.

Right click SunSky in World Outliner and Browse to Asset.

Dublicate SunSky and replace SunSky from level with dublicate.

Open Level Blueprint. I’m using Third Person Example.

Create two variables in level blueprint.

  • GameTimeUpdateDelay is 1,0
  • ServerGameTime is Replicated

Add Switch Has Authority to BeginPlay. Set ServerGameTime variable in Authority.

Create Timer and Event. Use Date Time + Timespan to add time into Date Time.

Drag SunSky from outliner into level blueprint. Now we don’t yet have FromDateTime function from SunSky so create everything you can and come back.

Click for bigger.

Make your level look like this, so remove Directional Light, Skylight, Atmospheric Fog…

Then Build Lighting. Level is going to be pitch black but that’s what we wan’t for now.

Then Edit SunSky (the one you dublicated and added to level).

Match your level content to this list.

Now in SunSky add function named FromDateTime.

FromDateTime will have one input variable:

Then break it like this into year, month, day and solar time of SunSky variables.

Click for full resolution.

Unfortunately there is a small bug here. Because Solar Time is 0/100 and date time is 0/60 it’s going to skip like 40, this will create Sun skipping/jumping effect at the change of every hour and that is just going to look terrible. So i came up with this, super smooth, zero skipping time conversion function:

Click for full resolution. Milliseconds are usually 0.0 so ignore that one.

There’s a small gap between 99.2 (when 59.59) / 100 (60) so what that booster does it goes through values 99.3, 99.4, 99.5, 99.6, 99.7, 99.8 and 99.9, before 59.59 is met.

Go back to level blueprint and add FromDateTime function where it belongs.

Also set enable Replicates so we don’t forget it.

You may wan’t to set DirectionalLight Intensity to 10 lux

or Enable Extend default luminance range in Auto Exposure settings.

Sun and Sky Actor

Now you can test it. Set Net Mode “Play As Client”-

Now this has a small bug, Game Time is not accurate. The Game Time should change exactly 10 minutes every 1 seconds but sometimes it’s 10 more or less. This is not what we wan’t.

The time should be updated every frame, or at least the time on server. We need a static starting point where we can calculate the time. Then we send this value to clients and they can calculate it from there without server constantly spamming it to clients. This static start point is going to be real world UTC time. Then we should have rules which are replicated so if admin wan’ts to change time in real time it can happen, or make time go faster or slower.

Sometimes UTC time can be off by one or two seconds between server and client so first we have to sync UTC time on clients with the server.

For time sync we need Player Controller Class so create a new one:

Put it in Gamemode folder.

Open it and enable Replicates.

In DN_PlayerController we create one variable, event dispatcher and function.

TimeDifferenceToServer is Timespan:

ClientTimetoServerDifference function:

Then we have two events GetServerTime (Run On Server, Reliable) and ReceiveServerTime (Run on owning client, Reliable):

Then create event SendDifferenceToSunSky:

We’ll come back to this once we finish coding.

Then we have BeginPlay and event dispatcher:

Now create new Game Mode. Add New -> Blueprint Class -> Game Mode Base.

Put it in Gamemode folder.

Then open it and Select Class Defaults and Set Player Controller Class to DN_PlayerController.

And go to Project Settings.

In Project -> Maps & Modes -> Set Default GameMode to DN_GameMode.

Pages: 1 2 3 4
Subscribe
Notify of
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
kilokillzall

where did that “Update SunSky” function come from? is that an older version of Sun Position Calculator?

kilokillzall

oh shoot posted this on the wrong page

lvlmaster

Do you mean “Update Sky”? Because i can’t find “Update SunSky”.

Unmodified version looks like this:
comment image
In 4.26 and 4.27

This was probably made in 4.25. Because Epic added “Real Time Capture” into SkyLight
comment image

https://docs.unrealengine.com/4.27/en-US/WhatsNew/Builds/ReleaseNotes/4_26/#real-timeskylightcapture

Which is why “Update Sky” is not in 4.26 and latest:
comment image

bonkimaslo

Do you think this will be the proper system for Unreal Engine 5 as well? Or did they add some new way to do it even better?
Thanks for your hard work

bonkimaslo

For some reason in UE5 you can’t add Make Timespan and Date Time as they are incompatible

lvlmaster

Make sure you got the variable type right, best way to do variables is to use “Promote to Variable” which you can get by pulling out from endpoint.
In this case find AsTimespawn (text) or AsTime (text) and try pulling out from these to create variables.

https://docs.unrealengine.com/4.26/en-US/BlueprintAPI/Utilities/Text/AsTime/

https://docs.unrealengine.com/4.26/en-US/BlueprintAPI/Utilities/Text/AsTimespan/

bonkimaslo

Thanks for the quick responses, appreciate it!

It seems like a UE5 bug to be honest.
When I drag Server Game Time to A position (Date Time structure) of the ADD node, the B position shows that it’s timespan structure, which it should be. But when I connect the Make Timespan endpoint to it, the A (Server Game Time) connection is broken, and it changes every pins structure type to timespan.
I found some other post showing the same problem :
https://forums.unrealengine.com/t/adding-timespan-to-date-time-not-working/256329

Sorry to spam your tutorial about UE5 stuff, I might have to build it in UE4 and migrate to UE5 to make it work

lvlmaster

Oh yeah, i found the same thing from UE5 EA2, it’s actually missing Date Time and Timespan combining/add node.

You could create Blueprint Function Library in C++ and then create custom function which combines them and returns result or… just try the full C++ version:
https://levelparadox.com/2020/11/13/ue4-day-and-night-cycle-c-part-2/

you just need to create material, material instance and 2 curves and it should work out of box.

Add me on Discord if you need more help: nuclearlocket#9741

bonkimaslo

Haha, I waited with messing around with it until the actual release of UE5, but it seems like they didn’t fix this issue, this node is still bugged for me.
I have no experience with C++ in Unreal, but gonna give it a shot, thanks!

lvlmaster

Well haven’t seen anything new or better anywhere else and seem’s like everyone else is also using Sun Position Plugin tools to do Day Night Cycle in UE5. Even in UE5 + Lumen you still need Sun Position Plugin, Exponential Height Fog and Sky Athmosphere if you wan’t Dynamic Day Night Cycle.

This project in UE5:
https://www.youtube.com/watch?v=nkPnIlPtg8Y

Someone else using Sun Position Plugin in UE5:
https://www.youtube.com/watch?v=kcV5XOqX9i8

Level Paradox
10
0
Would love your thoughts, please comment.x
()
x