Search Unity

Measuring time passed in seconds.

Discussion in 'Scripting' started by lukaziomal, Aug 24, 2016.

Thread Status:
Not open for further replies.
  1. lukaziomal

    lukaziomal

    Joined:
    Jul 16, 2016
    Posts:
    5
    Hey guys, so basically I need to measure time between 2 times in seconds. I need that for clicker game,to make like:
    money += timepassed * moneypersecond
    And of course I need to save time A on Quit, and measure time B on start and then difference between them. I tried to make it on my own but everytime when I was running app on phone I was getting like 12000000 money.
    Any advice?
     
  2. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    359
    Getting the time difference in seconds is just

    Code (CSharp):
    1. var diffInSeconds = (dateTime1 - dateTime2).TotalSeconds;
    Don't forget Time.deltaTime as well
     
    trombonaut likes this.
  3. lukaziomal

    lukaziomal

    Joined:
    Jul 16, 2016
    Posts:
    5
    so this is my saving code, this code is working perfectly, but im not sure where should i put, your part of code. LastRun is System.DateTime variable in Data class and i want to measure it and then calculate the difference.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using System;
    5.  
    6. public class BackGroundSaving : MonoBehaviour
    7. {
    8.  
    9.     public Data data;
    10.     System.Diagnostics.Stopwatch czasomierz = new System.Diagnostics.Stopwatch();
    11.     public Data2 data2 = new Data2();
    12.  
    13.     void Start()
    14.     {
    15.         try
    16.         {
    17.          
    18.             Load();
    19.  
    20.  
    21.  
    22.  
    23.         }
    24.         catch (Exception)
    25.         {
    26.  
    27.         }
    28.  
    29.  
    30.         czasomierz.Start();
    31.  
    32.  
    33.  
    34.     }
    35.  
    36.     public void Save()
    37.     {
    38.         data2.wspolczynnik_zarabiania_pieniedzy_na_sekunde_TradeMark = data.wspolczynnik_zarabiania_pieniedzy_na_sekunde_TradeMark;
    39.         data2.wspolczynnik_zarabiania_pieniedzy_na_klikniecie_TradeMark = data.wspolczynnik_zarabiania_pieniedzy_na_klikniecie_TradeMark;
    40.         data2.LastRun = data.LastRun;
    41.         data2.money = data.money;
    42.         Saver.SaveData(data2, "save.bin");
    43.  
    44.  
    45.     }
    46.     public void Load()
    47.     {
    48.         Saver.LoadData(ref data2, "save.bin");
    49.         data.wspolczynnik_zarabiania_pieniedzy_na_sekunde_TradeMark = data2.wspolczynnik_zarabiania_pieniedzy_na_sekunde_TradeMark;
    50.         data.wspolczynnik_zarabiania_pieniedzy_na_klikniecie_TradeMark = data2.wspolczynnik_zarabiania_pieniedzy_na_klikniecie_TradeMark;
    51.         data.LastRun = data2.LastRun;
    52.         data.money = data2.money;
    53.  
    54.  
    55.  
    56.     }
    57.    
    58.     void OnApplicationFocus(bool pauseStatus)
    59.     {
    60.         if (pauseStatus)
    61.         {
    62.  
    63.         }
    64.     }
    65.  
    66.  
    67.  
    68.  
    69.     public void Update()
    70.     {
    71.  
    72.         if (czasomierz.ElapsedMilliseconds >= 10000)
    73.         {
    74.             Save();
    75.             czasomierz.Stop();
    76.             czasomierz.Reset();
    77.             czasomierz.Start();
    78.  
    79.         }
    80.         if (Input.GetKey(KeyCode.Escape))
    81.         {
    82.  
    83.             Save();
    84.             Application.Quit();
    85.         }
    86.     }
    87. }
     
  4. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    359
    No worries,

    You now know how to calculate the seconds using .TotalSeconds, so you just have to decide where you want to put it :) I think you're better equipped on that decision than I am. None of the time fields you are working with are in the data structure above, so I can't tell you where to put the code. Sorry.
     
  5. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    As an example for you:
    Code (CSharp):
    1. public class Test1 : MonoBehaviour {
    2.  
    3.     public System.DateTime startTime;
    4.  
    5.     void Start () {
    6.         startTime = System.DateTime.UtcNow;
    7.     }
    8.    
    9.     void Update () {
    10.         System.TimeSpan ts = System.DateTime.UtcNow - startTime;
    11.         Debug.Log (ts.Seconds.ToString ());
    12.     }
    13. }
     
    user_spartans, thefish2191 and yyylny like this.
  6. lukaziomal

    lukaziomal

    Joined:
    Jul 16, 2016
    Posts:
    5
    Code looks like this now, but it is giving me 120000000 at start. What is the problem here?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using System;
    5.  
    6. public class BackGroundSaving : MonoBehaviour
    7. {
    8.  
    9.     public Data data;
    10.     System.Diagnostics.Stopwatch czasomierz = new System.Diagnostics.Stopwatch();
    11.     public Data2 data2 = new Data2();
    12.     public System.DateTime startTime;
    13.  
    14.     void Start()
    15.     {
    16.         try
    17.         {
    18.             startTime = DateTime.Now;
    19.          
    20.             Load();
    21.             if (data.diff > 0)
    22.             {
    23.  
    24.  
    25.                 data.money += data.diff * data.wspolczynnik_zarabiania_pieniedzy_na_sekunde_TradeMark;
    26.             }
    27.  
    28.         }
    29.         catch (Exception)
    30.         {
    31.  
    32.         }
    33.  
    34.  
    35.         czasomierz.Start();
    36.  
    37.     }
    38.  
    39.     public void Save()
    40.     {
    41.         data2.wspolczynnik_zarabiania_pieniedzy_na_sekunde_TradeMark = data.wspolczynnik_zarabiania_pieniedzy_na_sekunde_TradeMark;
    42.         data2.wspolczynnik_zarabiania_pieniedzy_na_klikniecie_TradeMark = data.wspolczynnik_zarabiania_pieniedzy_na_klikniecie_TradeMark;
    43.  
    44.         data2.diff = data.diff;
    45.         data2.money = data.money;
    46.         Saver.SaveData(data2, "save.bin");
    47.  
    48.  
    49.     }
    50.     public void Load()
    51.     {
    52.         Saver.LoadData(ref data2, "save.bin");
    53.         data.wspolczynnik_zarabiania_pieniedzy_na_sekunde_TradeMark = data2.wspolczynnik_zarabiania_pieniedzy_na_sekunde_TradeMark;
    54.         data.wspolczynnik_zarabiania_pieniedzy_na_klikniecie_TradeMark = data2.wspolczynnik_zarabiania_pieniedzy_na_klikniecie_TradeMark;
    55.         data.diff = data2.diff;
    56.         data.money = data2.money;
    57.  
    58.  
    59.  
    60.     }
    61.    
    62.     void OnApplicationFocus(bool pauseStatus)
    63.     {
    64.         if (pauseStatus)
    65.         {
    66.  
    67.         }
    68.     }
    69.  
    70.        public void Update()
    71.     {
    72.  
    73.         if (czasomierz.ElapsedMilliseconds >= 10000)
    74.         {
    75.             data.diff = (DateTime.Now - startTime).TotalSeconds;
    76.  
    77.             Save();
    78.             czasomierz.Stop();
    79.             czasomierz.Reset();
    80.             czasomierz.Start();
    81.  
    82.         }
    83.         if (Input.GetKey(KeyCode.Escape))
    84.         {
    85.             data.diff = (DateTime.Now - startTime).TotalSeconds;
    86.             Save();
    87.             Application.Quit();
    88.         }
    89.     }
    90. }
     
  7. lukaziomal

    lukaziomal

    Joined:
    Jul 16, 2016
    Posts:
    5
    OKAY SORRY FOR SPAM, GUYS THANKS FOR ALL HELP I FIGURED IT OUT AND NOW IT WORKS.
     
    panSeler likes this.
  8. amit-

    amit-

    Joined:
    Jun 16, 2018
    Posts:
    2
    then post answer also..
     
  9. franklySo

    franklySo

    Joined:
    Jun 6, 2019
    Posts:
    1
Thread Status:
Not open for further replies.