Saving Scores: Mostly Complete

The scoring system now can save to disk, and each level has its own save file.  I have only tested on my Windows PC, need to try on my android device next.  In theory it should work.  We’ll see.

The one issue I have with it is that if you have a new score that equals another older score (but can push down an even lesser score), it will push down the lesser score, but it appends itself AFTER the 1st score that is equal to it:

(Pretending I have a max of 3 on the table – I really have 10, but for simplicity’s sake):

Dave 40,

Bob 30,

Joe 20.


Jim just got 30.


The list should be :
Dave 40

Jim 30

Bob 30


But it actually renders as:

Dave 40

Bob 30

Jim 30


Joe is pushed off as he should be, but the older high score remains above the newer one.  Perhaps that’s as it should be.  But I feel like the newer one should take precedence.  I suppose I could also try to sort by name as a secondary sort after score.  But since people type in their name, then they can just choose to be AAA or whatever.

I’ll have to look into score sorting and see how other games do it – I don’t think I really paid much attention to that detail before.

Serialization: Successful – Rendering: Not So Much.

Today I started the process of getting the scoretable to serialize.  So far, what I’m doing is

1) Load Scores from Disk, if the table for that level exists.
2) Get User Name
3) Set UserName
4) Set ScoreEntry (Username, Hexagon Score, HoneyScore)
5) Sort Scores
6) Save Scores (this includes previously saved scores that were loaded, so the new score is added to the list and sorted (in theory))
7) (for testing purposes) ResetScores to 0, so it’s fresh when I load from disk — maybe not a bad thing to do in general, I dunno.
8) Load Scores from Disk
9) Display the Scores

Mostly it seems to work, except once I hit the limit, somewhere along the way things aren’t put into order. Let’s say my last entry has a score 7, then 15, 18.  Now I add a 23 score.  Instead of pushing 7 down, it removed 15, and left 7.

However, next time I loaded it and played and did this, the 7 was removed, and 15 reappeared at the bottom (IIRC).

I’ll have to play a lot with this to see what exactly is happening.  My gut instinct is that somehow that last entry is not being deleted/sorted (which doesn’t make sense, as I’m not loading by indexing, but using the built in List features of C#)  If it were my own indexing/sorting routine, I would feel more likely that this is the case.

Since this is the case, perhaps it’s my rendering method, since that is pretty much my own code.  I’ll have to look there.  It’s clear that the data is still in the file, since it showed up in a subsequent run.  Wondering if I should try text serialization first before I do the binary just so when I look at the entries in the save file, I can make sense of them and see what’s actually being written to disk (vs the binary format which obfuscates the data).


Score System: Serialization to Disk

So – I got the score system working so far.  All the rendering issues seem to have been worked out.  The code is easier to read than it was, as well.

Now, my next goal is to save the data to disk.  Unity has a special feature called “PlayerPrefs” which is a file that’s basically a hash.  It contains methods that serialize data, either string, int or float.  However, there are no higher order structures.

So I could write the player name, and the scores, but if I wanted to write another one, I’d either have to append something like the save number to each new one (i.e. dave1, dave2) which is dumb, or I can use a few other options.

The first is one shared on the Unify Community wiki, and that’s called ArrayPrefs2. That’s a nice little utility that allows you to serialize more than those features, including things like booleans and arrays.  Since my scoretable is a list, I could try to use this (using C# “ToArray()” method of the List datatype).  However, on a quick glance, I don’t know how it would deal with arrays/lists of structs (which is what my score is: name, hexscore, honeyscore).  I’m not sure if it supports the ability to save multiple levels of indirection like that.  In theory it should, but I’ll have to research some more.

The other option that looks nice is Json.NET.  This is an open source package for .Net/C# that makes serializing data in the JSON format quick and simple.  There is also some built in serialization/JSON utilities in C#, but it seems like a more complex API for JSON.  Since I’m not doing anything super heavy, I’d prefer something more lightweight and simple.

Once I can get the data serialized either via arrayprefs2 or, I’ll have to figure out how I want to store it.  I guess I can then stringify the data to be stored in playerprefs, and then when I want to load it, have a process that deserializes the data.  Or I might be able to use a filestream to save the serialized data to disk.  I know everyone says playerprefs is Unity’s way to do things, I just don’t know if it’s really what I’m looking for.  It might, I’ll just take that aspect as it comes.  First I’ll try to get the score table serialized in memory before worrying about saving to disk.