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
You can use DateTime and TimeSpan structs to calculate and store this type of data.
using System;
// start activity (take timestamp)
DateTime begin = DateTime.Now;
// time goes by
// activity ends (take timestamp)
DateTime end = DateTime.Now;
// calculate timespan
TimeSpan timeSpent = begin - end;
// or manually create TimeSpan
int days = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
TimeSpan span = new TimeSpan(days, hours, minutes, seconds);
Note you can also add timespans together to display aggregate results.
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?
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:
// save a timespan
TimeSpan span = new TimeSpan(2, 24, 6);
PlayerPrefs.SetString("duration", span.Ticks.ToString());
// load a timespan
string ticksString = PlayerPrefs.GetString("duration");
long ticks = long.Parse(ticksString);
TimeSpan oldSpan = TimeSpan.FromTicks(ticks);