Search Unity

What to choose to store and reading data by Year, monthly, weekly and day?

Discussion in 'Scripting' started by Tapgames, Aug 8, 2016.

  1. Tapgames

    Tapgames

    Joined:
    Dec 1, 2009
    Posts:
    242
    Hi All,

    I need some help to decide what I need to use to store and read data like below.

    I'm building a little game that connects to a bluetooth LE fitness bike and what i'm getting is the following data the speed, wheel rpm, time biked, calories burned and distance biked. Now when the player has biked for lets say one hour I like to store some data "The date, time biked, calories burned and distance biked". The player can do this a couple of times a day so there can be a couple of saves a day.

    Now I want to make a menu called "Overview" and a few buttons Day, Weekly, Monthly and yearly to show the stored data to the player by day, week, month and year. So the player can see how many calories, distance and time the player has biked per day or combined weekly, monthly and year in a simple menu.

    Any guidance in how to set this up?

    Thanks,

    --Roy
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You can use DateTime and TimeSpan structs to calculate and store this type of data.

    Code (CSharp):
    1.  
    2. using System;
    3.  
    4. // start activity (take timestamp)
    5. DateTime begin = DateTime.Now;
    6.  
    7. // time goes by
    8.  
    9. // activity ends (take timestamp)
    10. DateTime end = DateTime.Now;
    11.  
    12. // calculate timespan
    13. TimeSpan timeSpent = begin - end;
    14.  
    15. // or manually create TimeSpan
    16. int days = 0;
    17. int hours = 0;
    18. int minutes = 0;
    19. int seconds = 0;
    20. TimeSpan span = new TimeSpan(days, hours, minutes, seconds);
    https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx
    https://msdn.microsoft.com/en-us/library/system.timespan(v=vs.110).aspx

    Note you can also add timespans together to display aggregate results.
     
  3. Tapgames

    Tapgames

    Joined:
    Dec 1, 2009
    Posts:
    242
    Thanks for your help Jeffrey!

    The question really is, should I use a database to store that kind of data? Or is there another way of doing this?
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    If this is all within Unity and offline, you could use PlayerPrefs to store the TimeSpan Ticks. Then use TimeSpan.FromTicks to get and restore that data from PlayerPrefs.

    A Tick is the smallest representation for time. 1 Tick is 100 nanoseconds I believe, so you can use Ticks to maintain accuracy.

    For Example:
    Code (CSharp):
    1. // save a timespan
    2. TimeSpan span = new TimeSpan(2, 24, 6);
    3. PlayerPrefs.SetString("duration", span.Ticks.ToString());
    4.  
    5. // load a timespan
    6. string ticksString = PlayerPrefs.GetString("duration");
    7. long ticks = long.Parse(ticksString);
    8. TimeSpan oldSpan = TimeSpan.FromTicks(ticks);