- Home /
Using real-world date and time for in-game time mechanics
I'm trying to make a game similar to the recent XCOM remakes, and I want to recreate the time mechanics in those games' overworld maps. If you haven't played those games, basically I want to:
Get the real-world time at the moment the player starts a new game.
Automatically advance that starting time forwards by a certain amount at the start of the game (for example, moving the time 10 years ahead of the game's start).
Have that time advance at the same rate as real-time (1 in-game second to 1 real second), while also having the option of fast-forwarding or freezing the time at need.
Have the game trigger functions based on certain criteria (at the end of each month, for example).
Basically, I want to store the real-world date and time as a variable at the start of the game, then spend the rest of the game altering and checking that variable arbitrarily. How could I go about doing this?
Answer by tanoshimi · Oct 14, 2016 at 12:09 PM
System.DateTime.Now gives you the real-world time on the user's computer. Everything else you specify can be derived from that.
How would I store that value, though? Is there a specific variable for date/time?
Yes, DateTime. The first line of the description on that page states: "The Now property returns a DateTime value". And the first line of the example code shows a DateTime variable used to store that value:
DateTime localDate = DateTime.Now;
Ah, my apologies. I should have looked more carefully.
One further, slightly off-topic problem: I've made a system that allows me to speed up, slow down, and freeze the "global" time, and I'm currently trying to make all other in-game timers sync to it. I'm trying to make all timers store a reference to the WorldTimer script, so they can efficiently check the timeFactor variable every Update. But for some reason the code refuses to recognise the component variable. $$anonymous$$y script is below:
public int timeFactor;
public Component worldTimer;
void Start () {
worldTimer = gameObject.GetComponent<WorldTimer> ();
timeFactor = worldTimer.timeFactor;
}
The problem is with the timeFactor = worldTimer.timeFactor line. The code doesn't seem to recognise what worldTimer is referring to, and so refuses to acknowledge the timeFactor variable within the WorldTimer script. How can I fix this?
A couple of things that you might want to look out for, the importance of which will depend on the what exactly you're doing with the time in your game (and maybe also on the platform)....
1) People may be able to set the system clock. If this could matter in your game, you might want to connect to a server periodically to get the real time.
2) System.Time.Now is the local time. If this could matter in your game (eg if you don't want the duration of an effect to depend on whether or not they've changed time zones), you might want to use System.Time.UtcNow ins$$anonymous$$d (which gives you Greenwich $$anonymous$$ean Time).
Your answer
Follow this Question
Related Questions
Get System date 1 Answer
Cleaning this code up. 2 Answers
Count time when the game is not running (mobile device)? 2 Answers
How can I get the time since the epoch date in Unity3D? 2 Answers
putting the date into a variable 1 Answer