In-Game Clock

Hello all,

I am a relatively new user of unreal engine and I’m having a lot of problems with the date/time system. My goal is to get a non-real world time system that I can use to schedule NPC events, day/night cycles, et cetera. I was able to create what I wanted for the most part, but hit a road block. After about a week of pulling out my hair, I’ve come to seek your assistance.

I have a global instance named GI_Global wherein I calculate and store the variables using a function named UpdateTime. I also have an actor named BP_Timer in my scene that actually does the raw counting, and I have a widget named W_DateTime which outputs the date and time to different text blocks. I’ve included all three for clarity, but I’m positive the problem lies in GI_Global.

Above is how I would like it to look. The problem is the date/time resets to Jan 1, 0001 at 12:00:00am and stays there when I let the clock run for 24 hours, or if I have the days node attached. Below is how it looks when that happens.

The print-string is displaying the Date/Time from GI_Global plus the raw seconds coming from the BP_Timer actor.

Above is how my blueprint looks when the clock works (before hitting the 24 hour mark) and below is how it looks when it doesn’t work at all.

As you can see, the only thing I’ve changed between the two is connecting the Set Days node to the sequence node.

A web search revealed that it may have to do with converting the time from Gregorian calendar to UTC calendar. But I don’t think I’ve done that here.

Below are the additional components I’m using just in case there is a problem I don’t see with them. The first is the actor in my scene that does the counting, and the second is the widget blueprint bound to the text object for time (the one for date is the same except it outputs ‘As Date’ instead of ‘As Time’).

I hope I’ve provided enough information for someone to steer me in the right direction. I’m also open to doing this a completely different way.

Thank you in advance for your time.

Welcome @RevBushido

You came to the right place. Lets jump straight into it. Here’s an example of what you could do.
Maybe that will get you started

For this example I do this in my player, but you can do it anywhere you like.

On Begin Play I set my initial date, in the future if you plan on keeping track of it between sessions using save games, you would set it from there.

Then we start a timer that will “tick” every second and call function “Clock”

In Clock function we simply add 1 second to our current date time.

Event Tick with print string for debug (Video).

That way you can set whatever date you please with ease and control how the time is passing.

As for assigning tasks to NPCs at certain time, I’m not exactly sure what is the best setup, but you definitely don’t want everyone to have a timer on them, so you would need to come up with a way (or look one up) to do it.

Video (YouTube)

1 Like

Tick will give you delta time, and a clock is a pretty good use of Tick() it is counting the entire time.

  • Tick() is not evil, it is easily overused. you can even constrain the tick to respond every second, it might be delayed by a little bit, but it will be around 1 second.

computers have been using real time for a long time. if you want to trust the system time then you can use the “Now” node which will steal the exact Date Time data from the system clock. you can also get this in UTC time instead of Local time with “UTC Now”. (this is a short hand of DateTime->Now() )

if you don’t want to trust the system clock in terms of “what is the real data and time”, but the deltas are often “safe” you can use the deltas of two different “Now” values with just a subtraction between Date/time nodes. This operation is optimized enough that doing it in a Tick() even unconstrained will be fine. so you can store the games DataTime in the save file, then on load store a “Now” in a local variable, and then in the Tick() get the “Now” take the delta, and add that to your saved DateTime.

  • if you still don’t want to trust the delta value (the player changes their system clock, or daylight savings switch happens) you can include your sanity checks before modifying the stored value.
  • you might have an issue with people playing your game on a 32 bit system in the year 2038, but they have other issues
  • the 64 bit system comparison to the “2038 problem” is supposedly a few thousand years off, and we might be on 128 bit systems by then.

do you “need real time scale” in your game? remember that the majority of people that play games might only have anywhere from 30 minutes to a couple hours to play, and it might not be at the same time every day, and it might not even be multiple days in a row.

also real world time scale is slooooooooow the notion that “time flies when your having fun” if there is stuff bound to real world time scale, and other times you are going to have downtime, and the world “boring” might happen. it is up to you for your time scale at least with regards to “time of day”, but I would strongly suggest anywhere from 1 RealWorldHour=6 InGameHours, to even 1 RealWorldHour = 1 InGameDay. depending on your event timelines, your traversal speed, and what your NPCs are doing.

think about the geography of your world:

  • how far apart are these 2 places: are they 10 Kilometers, are they 100 kilometers
  • how fast does the character move relative to a real human (an adult human is somewhere between 4->6 Km/Hr).
  • do you have variable speeds when walking (is there an encumbered or “out of stamina” speed), how fast do they run, how long can they run (will your players care)