- Home /
Serializing a Timespan
So I am expanding on Roll a Ball and I was adding a high score system via a stopwatch and I found out that you can't serialize a timespan, directly at least. So I was wondering if there was a way around it. However, I could just have the code wrong. What happens is when I try to print the timespan after supposedly saving it, it just gives me 00:00:00.00. Here is the code.
My Save/Load function
public void Load(){
if (File.Exists (Application.persistentDataPath + "/ball.cid")) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/ball.cid", FileMode.Open);
Data col = (Data)bf.Deserialize (file);
file.Close ();
Colornum = col.Colornum;
hs = col.hs;
print ("Loaded");
}
}
public void Save(){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create (Application.persistentDataPath + "/ball.cid");
Data col = new Data ();
col.Colornum = Colornum;
col.hs = hs;
bf.Serialize (file, col);
file.Close ();
print ("Saved!");
}
}
[Serializable]
class Data{
public int Colornum;
public TimeSpan hs;
}
My text function
void Start () {
string highScore = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
SavingLoading.hi.hs.Hours, SavingLoading.hi.hs.Minutes, SavingLoading.hi.hs.Seconds,
SavingLoading.hi.hs.Milliseconds / 10);
score.text = highScore.ToString ();
print (SavingLoading.hi.hs);
}
My Other Text Function
void Start () {
SavingLoading.hi.mt = SavingLoading.hi.lvl1.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
SavingLoading.hi.mt.Hours, SavingLoading.hi.mt.Minutes, SavingLoading.hi.mt.Seconds,
SavingLoading.hi.mt.Milliseconds / 10);
string highScore = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
SavingLoading.hi.hs.Hours, SavingLoading.hi.hs.Minutes, SavingLoading.hi.hs.Seconds,
SavingLoading.hi.hs.Milliseconds / 10);
time.text = "You took: " + elapsedTime.ToString();
hs.text = "Highs Score: " + highScore.ToString ();
if (SavingLoading.hi.mt < SavingLoading.hi.hs) {
SavingLoading.hi.hs = SavingLoading.hi.mt;
print (highScore);
}
SavingLoading.hi.Save ();
Any help is appreciated! PS: If you need any other info, please tell me.
Sorry but it's not clear when and where you actually run your Load method. Are you sure you actually load your highscore before you try to display it? Using the BinaryFormatter seems a bit overkill unless you explicitly want to store the highscore in a file that way. If you have a TimeSpan value you can just use it's "Ticks" property to get the ticks it represents. This long value can be stored and used to recreate a TimeSpan.
Answer by JimmyCushnie · Feb 02, 2018 at 05:18 AM
I think you're overengineering this. I would keep the score as an integer or float representing the number of seconds; this is easier to do math with and serialize. You can then convert it to the formatted string you want only when it is displayed to the player.
That's what he does. The TimeSpan is just a struct that contains only a single long value representing the ticks that have passed.
Your answer