Time System - am I thinking this through correctly?

Hi all,

I’m moving from UDK to UE4 and need to transfer my code over. I thought that knowing C would help me here, but the difference between C and C++ is much larger than I thought it would be, though I suspect most of my hiccups are coming from my lack of familiarity with the UE4 codebase itself.

Anywho, I would like some feedback on the following (i.e., am I thinking about this correctly, is there a better way of doing it, etc.):
I need a Time-of-Day system which can track and work with spans of time ranging from minutes to thousands of (ingame) years. The UDK version of this system was singleplayer-only, but I’d like to give multiplayer a shot here - or at least leave the option open - so this time, I’m worried about replication. I think that the best way to go about things is to create a Time Manager or Timekeeper that resides only on the server, and updates the in-game time either once per tick (probably inefficient and generally overaccurate) or via a timer with a predefined interval, like 15 or 20 times per second. This timekeeper would then send the calculated timestamp (a custom class containing an array of ints corresponding to year, month, day, hour, and minute, and a handfull of functions to compare, add, or subtract timestamps) to the GameState, which would then be replicated out to connected clients. Any movement of the sun or moon would also be calculated server-side and pushed to GameState for replication. On the client side, when GameState detects an update of the Timestamp, it pushes out the new data: human-readable time gets printed to HUD, the sun and moon get translated to their new positions, etc.

Is this a good way of doing things? Is there something more efficient?

Also, I haven’t found any kind of ElapsedTime variables or functions yet; is there any built-in way of getting the length of time elapsed since the game has started (not counting paused time), or do I need to spawn and use my own timer when the game starts?

Thanks in advance.

Anyone? I’d appreciate any input.

In UWorld you get “TimeSeconds()” which is “Time in seconds since level began play, but IS paused when the game is paused, and IS dilated/clamped.”

this should help you out, or is at least a starting point. There are also a few other variables in UWorld that works with the game time, take a look there and see if any of them might help you out a bit.

I just saw “GetRealTimeSeconds” in UWorld, it’s the Time in seconds since level began play, but is NOT paused when the game is paused, and is NOT dilated/clamped. This might be what you are looking for.

There whole time framework in FDateTime

You also got timers

This is awesome !! Thanks, I’ll also use this.

Thanks, guys!

FDateTime isn’t exactly what I want since I’ll be rolling my own, but that’ll come in really handy as a starting point.

Alright, I got my time system somewhat working using a few actors. My current setup looks something like this:

> Timestamp - actor that contains a time and can be set, added to or updated.
> GameState.CurrentTime - a timestamp that describes the current time in the ingame world (planned, world timestamp is currently inside Timekeeper).
> Timekeeper - actor calculates world time per-tick, and updates GameState.CurrentTime if need be. determines the length of ingame time periods (year, month, etc) and the realworld-to-gameworld time conversion rate. exists only on the server.

Currently, the Timekeeper is placed manually in the world and assigned values in the editor. I had hoped to merge parts of the timekeeper with the WorldSettings so that the World Settings panel in the editor would contain the lengths of time periods, rather than having to place an actor in the world to change them from their code defaults, but that ended in complete disaster: Why does changing the World Settings and break my game? - UE4 AnswerHub and now I’m scared to even touch WorldSettings.
All I really need is somewhere with a BeginPlay that I can tap into to create and set the world’s timestamp; where would be a good place to do this?

Are you… are you creating Narnia?

Couldn’t you instantiate your magical timekeeper on the GameMode, so when the gameplay kicks in it’ll auto-spawn your timekeeper and you can interact with it from there?

Ehh… something like that.

Yes, I’ve hooked into GameMode::StartMatch() to do all my initializations, and I think spawning the timekeeper from there would be a good idea. I was trying to avoid that because I wanted to have more customizable options in the timekeeper, but until my issues with WorldSettings get sorted out, I think I’ll just have to hardcode some things.