Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

C# Keeping Time.

Discussion in 'Scripting' started by SaintNick, Dec 23, 2014.

  1. SaintNick

    SaintNick

    Joined:
    Mar 20, 2014
    Posts:
    4
    Hey guys, i've been looking at this one for awhile.

    I want to have a system for keeping track of game time. Right now I have this, fair warning, its not pretty.

    Code (CSharp):
    1.     IEnumerator TimeKeeping() {
    2.  
    3.         index = 0;
    4.         while (index < 7)
    5.         {
    6.             Debug.Log(index);
    7.             yield return new WaitForSeconds(0.4f);
    8.             index++;
    9.  
    10.             text.text = " " + index;
    11.  
    12.             if (index > 6){
    13.                 index = 0;
    14.                 week++;
    15.                 day++;
    16.  
    17.                 if (week > 3)
    18.                 {
    19.                     day = 0;
    20.                     month++;
    21.                     Debug.Log("New Month: " + month);
    22.  
    23.                     if (month > 11)
    24.                     {
    25.                         month = 0;
    26.                         year++;
    27.                         Debug.Log("New Year: " + year);
    28.                     }
    29.                 }
    30.             }
    31.         }
    32.  
    33.         Debug.Log("End Of Enumerator");

    Any help would be gladly appreciated, its 04:16 and i've spent quite a few late nights trying different variations of the above with no luck.

    Thanks Nick.
     
  2. Jim_West

    Jim_West

    Joined:
    Aug 5, 2013
    Posts:
    47
    What exactly do you want to save, overall played time of the game like in steam "played for 30 hours" etc. ?
     
  3. Mycroft

    Mycroft

    Joined:
    Aug 29, 2012
    Posts:
    160
  4. SaintNick

    SaintNick

    Joined:
    Mar 20, 2014
    Posts:
    4
    the time system will be used by other scripts to know when to do things, so X happens every 3 days, Y happends every 1 year. it would be the equivalent to a grand-strategy-game game clock
     
  5. Mycroft

    Mycroft

    Joined:
    Aug 29, 2012
    Posts:
    160
    You're definitely going to want to use DateTime and likely TimeSpan as well; they combine to handle exactly the situation you're describing.
     
  6. SaintNick

    SaintNick

    Joined:
    Mar 20, 2014
    Posts:
    4
    thanks a bunch guys, i'll have a look at this tomorrow i have the day off so i should be able to get something up and running. i'll post the code here when i've finished-or if i run into problems... Thanks again guys.
     
  7. SaintNick

    SaintNick

    Joined:
    Mar 20, 2014
    Posts:
    4
    Ok, I guess the problem I'm having is not knowing how I want to add and subtract resources. I don't know if i should make a new thread for it, so ill post here.

    Ok, so I have resources, like food.
    Sinks/Consumers- People.
    Sources-Farms.

    I don't know where to start, I can use resource -/+ some time interval *Time.deltaTime.
    this would take or add over time, but it updates every frame.
    I have the same problem with time.
    Do i update DateTime every frame?

    If anyone has ideas, I would love to hear them.

    Thanks, Nick.
     
  8. Atmey

    Atmey

    Joined:
    Nov 3, 2012
    Posts:
    88
    I am not sure if you tried DateTime as suggested.

    But you can use Time.timeSinceLevelLoad or Time.realtimeSinceStartup., as it is more reliable than Time.deltaTime, add your custom milestones and check against that.