- Home /
Days since first launch
I want to count the days since the first launch of my game. I'm not talking about playing time, I'm talking about the total days that have passed, to display some statistics when the player finishes the game. How can I do this?
I suppose it has something to do with System.DateTime, but I'm wondering exactly what I would have to implement.
The very first time the game was launched it would have to store that day's System.Datetime.Today in a variable (of type DateTime?) and then do the same at the day someone would finish the game and then subtract the two and get an integer as a result?
If so, how would I ensure that the date of the first launch would only be stored once and not every time the game is launched?
Answer by Lo0NuhtiK · Apr 03, 2014 at 11:21 PM
I wasn't exactly sure how to do this either until I read your question a few minutes ago and used some voodoo known as Google.
I threw together a script just now using PlayerPrefs that seems to be working. I changed my PC's date a few times to test it out. Anyway, here ya go.
Put this script (rename it to whatever, of course) on some kind of gameManager object you're using in your game.
public class WhateverScriptNameYouWant: MonoBehaviour
{
private static System.DateTime startDate ;
private static System.DateTime today ;
void Start()
{
SetStartDate() ;
}
void SetStartDate()
{
if(PlayerPrefs.HasKey("DateInitialized")) //if we have the start date saved, we'll use that
startDate = System.Convert.ToDateTime(PlayerPrefs.GetString("DateInitialized")) ;
else //otherwise...
{
startDate = System.DateTime.Now ; //save the start date ->
PlayerPrefs.SetString("DateInitialized", startDate.ToString()) ;
}
}
public static string GetDaysPassed()
{
today = System.DateTime.Now ;
//days between today and start date -->
System.TimeSpan elapsed = today.Subtract(startDate) ;
double days = elapsed.TotalDays ;
return days.ToString("0") ;
}
}
Then from whatever other script(s) you're using to show the days passed, you just call it.
eg :
void OnGUI()
{
GUILayout.Label("Days Passed : " + WhateverScriptNameYouWant.GetDaysPassed()) ;
}
Thanks for the answer, I haven't used PlayerPrefs too much until now (I've only used it for saving score during exit and retrieving it on relaunch) so I guess that's where I should be focusing. Reading through your code makes perfect sense.
Cheers..
Answer by dillon_kneeland · Apr 03, 2014 at 09:36 PM
You could store it in a text file that could be read by the game when they finish. And in order to make it do it only once you could have it check to see if the file was already there.
Hmmm. A text file seems easily editable, I'm not sure it's very practical..
Answer by KnightRiderGuy · Jan 05, 2017 at 12:58 AM
I tried this out but for some reason overtime I start up my game session the OnGUI spits out this insane number something like: 736333 instead of what I think I was saving with the SaveStartDate... I thought the way it was supposed to work was save the start date and then subtract the current date from the start date in the OnGUI... instead I get this each time
Answer by jmgek · Jan 05, 2017 at 02:59 AM
While @loonhawks answer is great that opens up a few hacks, The user could easily change their system time thus work around what you're trying to do. He even went on to test it that way.
I suggest you use his answer as a backup if the user has no Internet access, if they have Internet access get your date time from the Internet:
public static DateTime GetNistTime()
{
DateTime dateTime = DateTime.MinValue;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nist.time.gov/actualtime.cgi?lzbc=siqm9b");
request.Method = "GET";
request.Accept = "text/html, application/xhtml+xml, */*";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
request.ContentType = "application/x-www-form-urlencoded";
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); //No caching
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
StreamReader stream = new StreamReader(response.GetResponseStream());
string html = stream.ReadToEnd();//<timestamp time=\"1395772696469995\" delay=\"1395772696469995\"/>
string time = Regex.Match(html, @"(?<=\btime="")[^""]*").Value;
double milliseconds = Convert.ToInt64(time) / 1000.0;
dateTime = new DateTime(1970, 1, 1).AddMilliseconds(milliseconds).ToLocalTime();
}
return dateTime;
}
Thanks @ jmgek,
I really don't need anything that complicated... what I was looking for was a method of tracking how many days had passed since a certain question was asked.
I have an if statement that in the first part checks to see if the question was asked and if it was gives a response of "You Just Asked $$anonymous$$e That" If the question was not asked then it would ignore the first part of the if statement and go onto the 2nd part where a different response would be given, something like "No I'm afraid not"
But I'm having difficulty trying to figure out how to get my data saved into player prefs back into a usable format for my if statement.
I'm not really concerned if some tries to change their time information or even the player prefs document, the process does not need to be that secure as it's only to make the conversational aspects of the VR more realistic by checking things like system time... stuff like that so no one would feel the need to cheat that... at least I can't think of any reason that would make sense. ;)
Ah, okay thats fine, I was assu$$anonymous$$g you were needing data for persistent logic "similar to how fable 3 calculated investments when the player was not playing"
So what you're going to want to do is extend what @loonhawm said and create a class that could hold all your info, question asked, last time asked, correct answers, etc and serialize that class.
Questions{
Datetime lastTimeAsked;
String question;
//other properties of your question
}
Alternatively if you just need the question and date just make a dictionary:
Dictionary<datetime, string> lastquestion
;
https://msdn.microsoft.com/en-us/library/4abbf6k0(v=vs.110).aspx
Your answer
Follow this Question
Related Questions
Unity 3 Coming out? 1 Answer
how to load and save date to && from Internet ? 2 Answers
In game clock/Time 1 Answer
How can I get the time since the epoch date in Unity3D? 2 Answers