Skip to content

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

Accurate Day Night Cycle

Before we can add rest of the logic in SunSky we need 2 structures, these are DayNightCycle_Rules and UTCTimeDifference. Our system will be using UTC Time to calculate Game Time, we don’t want to use clients local time because then it wouldn’t be the same for every player and this would result in different sun positions for players. And yes, this supports multiplayer out of box but it will also works without server.

UTCTimeDifference Structure

DayNightCycleRulse Structure

RealWorldTimeInGameTime is the game world time duration (24 hour cycle).

Rest of the variables.

  • GameTimeNow Date Time
  • Sunrise Time span
  • Sunset Time span
  • IsNight_B boolean
  • DebugOn (optional)

Also add two event dispatchers IsNight and IsDay.

This one is UTCTimeDifference structure:

This is DN_Rules variable (Replicated):

Go back to DN_PlayerController and finish SendDifferenceToSunSky.

Now in SunSky add BeginPlay event and set DN_Rules. RealWorldStaticStartTime should be set only on server but in this case it doesn’t matter that much because we have time sync.

RealWorldTimeInGameTime is set to 1 hour but you probably want to set it to 1 minute for the first run to see if everything works correctly.

Drag and drop Event Dispatchers and then select Assign, this is the easiest way to bind event dispatchers.

Create SunIsDown event, this will help us identify when it’s night and day.

Click for full resolution.

Sunset function.

Sunrise function.

Go to UpdateSun function and set Sunrise and Sunset variables.

So what we want to do is make Game World Time do 100% in x Real World Time.

GameTimeNow variable is where Game World Time currently is. This is where we put the result.

100% of Game World Time is 24 hours.

Let’s define 5 minutes of Real World Time to Game World Time.

We have Real World Start Time and Game World Start Time. And then we have Real World Time Now.

All variables:

  • GameTimeNow (result)
  • RealWorldStart (06/10/2020 21.31.44)
  • GameWorldStart (06/10/2009 15.23.11)
  • RealWorldTimeNow (clock)
  • RealWorldTimeinGameTime 300 seconds

So in 5 minutes 24 hours happens in game.

We’ll take 50% off from that we’ll get 2.5 minutes and 12 hours or 43200 seconds. We can use this to check if our formula is correct.

How much game time has passed in one second? 86400/300 = 288 seconds or 4 minutes 48 seconds.

So 2.5 minutes have passed how much game time has passed? 43200 = 288 x (60 x 2,5) AND THERE WE GO!

GameTimeNow = (86400 / RealWorldTimeinGameTime) * (RealWorldTimeNow - RealWorldStart)

Then we put it all into UpdateRealTime event.

Click for full resolution.

Then add the function to event tick.

Debug printing function.

Things can get confusing if you try to print GameTimeNow because there is UTC version of AsTime and some people people (including myself many times) used that and then reported me that something was wrong with the sunset and sunrise.

And test it! The Sun should do a full circle in 1 minute with these settings (if you have 1 minute in RealWorldTimeInGameTime).

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