Saving, Loading, Scoring

As a last post / update on this project I’ll tell you about the very last work I have had to focus on during this past week. And that is scoring, and the ability to save and load these scores.

For anyone who hasn’t followed these post: We are currently in the process of developing a small endless shooter, which we’ve now given the name “Burn Cube Burn”. Players in this game must survive an endless onslaught of evil cubes, hellbent on killing the player. The game is simple in its mechanics:

Players are a small triangle that can move, shoot and teleport.

Cubes start moving in from the top of the screen and shoot a sort of area of effect attack at the players.

Players must dodge these attacks while attacking the cubes, killing them.

As the game goes on, more cubes spawn making it increasingly difficult.

This is the game at its core. And as we’ve reached the end of our development with only a few days left. We’ve worked on tidying things up around the game, as well as fixing the very last feature left. The scoreboard.

Now the scoreboard is there to display the top 5 players on that machine (no, it’s not a global stat, only local). The score is calculated from the cubes destroyed, the waves advanced and have a multiplier based in how many of the so called “combo objects” the player picks up.

(For reference: The combo objects are small cubes dropped by defeated cubes and increase the player damage when they’re picked up).

NewHighScore!

But this raises the question: “How do you save and load score data in unity?”.

Well, there are several ways to do it. I have personally looked at three (3) different ways to achieve this, the first one was “PlayerPrefs”:

Unity has an in-engine thing called the “PlayerPrefs”, it is (from my understanding) some sort of file which can save and load some custom information. This information can be shared or cut up into all the different scenes that are in the game.

The reason to why I didn’t use it was because of two (2) reasons:

  1. It was quite difficult to find concrete information on what the PlayerPrefs would actually save. From the look of things, it seemed like it stored scene based data, such as best time and scoring. Which in my case was sort of what I wanted, but as the game only uses two (2) scenes, it made me unsure if I could save multiple scores per scene or just the one.
  2. The information regarding PlayerPrefs where sporadic at best. And it was quite difficult finding exactly how to use it, and how it worked.

The second and third ways I found were very similar and thus I will tell you about them as a single unit:

Saving by creation of custom files.

During my escapades trying to find good ways to save data for unity to use I came across a video showing and detailing the exact way to save and load an array of information into a binary file.

For those who don’t know:

An array is a sort of box of information. The information is stored in elements, and each element is given a number in this box. So as an example:

An array of integers (that is whole numbers) can contain three (3) elements of an integer number.

That means that element [1] contains a number, element [2] contains a different number, and element [3] contains a third number. But all these elements and numbers are saved in the same place and are sort of connected in that they are contained within that specific array (or box).

A binary file on the other hand, is a file format were, when the file is given information it re-writes it into a binary form (1:s and 0:s) and saves it like a binary number sequence rather than what the information might actually be (say if it’s a word like “Hello”, it won’t actually say “Hello” in the file).

This was a more than favourable option to use as it would make cheating a lot more difficult in the game.

The third way of doing it was very similar, as it also required the creation of an array to hold the information. But rather than saving the information into a binary file, I was shown a way to save the information into an XML file.

XML is another format that is more akin to a text document.

This also had a very detailed tutorial on how to do it, and it was in fact extremely similar to the binary format, with the exception of a few specifics (such as telling it to specifically save in XML rather than Binary format).

In the end, I decided to try my hands at the binary. I figured if it already was so similar, then I might as well save it to binary and prevent the potential of cheating and manipulating the scoreboard.

That is not to say that the binary file would make it impossible, but it would make the process a whole lot more difficult.

Post-Programming thoughts:

As a last post-programming thought for this project I think I’ll talk about the difficult process of making this save/load function. It’s still not working properly (mainly because I think I’m trying to load the wrong thing). But I wouldn’t have gotten so far with this if I hadn’t taken my time to read up on what I was doing. Watching the whole of a near 40-50-minute tutorial and asking for help from more experience programmers.

The process of making a game, or coding in general can take a lot of time, and it’s important to remember that even if you do find a shortcut, that isn’t always the answer, sure you might gain some time now, but you’ll more than likely need to use double that saved time later, fixing the things that broke because you didn’t do it right the first time.

I hope you’ve found this short adventure entertaining, as well as maybe a bit educational.  As we close the curtains on this current project, and look onwards for the next!

Thank you for reading!

5 thoughts on “Saving, Loading, Scoring

  1. A very good blogpost I would say. You have a clear “What, How and Why” and it was a pleasure to read through. You write very clearly and I never had to look back and wonder what you meant with what you wrote. I think your blog is also good for beginners in unity or coding, since you explain very basic stuff throughout your post, but also give a fair warning that you are about to talk about basic things, so that experienced readers can skip those parts. All in all a very good post that covers what you did well. Good job!

    Like

    1. What I think you can do to improve your blog even further is to link more to sources and sites that have helped you during the weeks efforts. For example in you “Post-programming thoughts” you mention a 40-50 minute tutorial, maybe you could imbed a link to that tutorial? Other than that I really do not have that much to comment on!

      Like

  2. In this blogpost, Leon has talked about his experience with implementing a scoreboard to his game “Burn Cube Burn”, while also making it hard for a player to manipulate.

    In a clear sequence of Why’s how’s and what’s it is described how the journey from using one method to another led to Leon saving his user information inside of an XML document. This thinking process is easy to follow for me as a reader and contains a lot of useful information for someone like me who have yet to implement a scoreboard into my game. A blogpost that I could consider a helpful guideline for my future projects is for me: A really good blogpost.

    Leon fittingly ends the blogpost with him making a small conclusion for his journey so far that partly ends with this project but continues onwards with the next one.

    Overall a really nice blogpost, but I agree with Arvid that it should contain some sort of source/reference that you based your scoreboard system upon 🙂

    Like

  3. Hi Leon,

    I think you have written a very instructive blog post and it is really interesting reading it. You have described very well how and why you chose to use to save and load the highscore using the binary file, talking about the problems that you have had while using the other options. The structure and the text are clear and the whole post is easy to read. The content is quite easy to understand and is good that you have explained some technical terms, so that even who is not a programmer can understand.

    I personally don’t have anything negative to say apart of the fact that you could have mentioned some references to the tutorial video.

    As a plus, I really like the thought that you have explained and I totally agree with that. It’s very important to think about the whole code structure and make a plan before implementing something, in order to not waste more time later if it is wrong.

    Well done and good luck for the final version!

    Steven Kolankowski

    Like

  4. This is one of the best blogposts I’ve read during this semester. Although it was a bit long, it was very interesting and informative, and not boring at all.
    At first, you made the reader familiar with your project without the need to go back to any previous posts about it. Then, you introduced your subject and elaborated upon its importance for your project. And once the objective of your “article” was clear, you dwelled into the options you had and the reasons behind your selection of the way you decided to accomplish your task with.
    I already have a slight background in basic programming and I’m a bit familiar with arrays and binary “language”, but I didn’t need that knowledge to understand what you were talking about. You are aiming for a very large audience of readers and I believe you succeed in having it. You explained each and every term that you found could be new and not familiar to people who have no idea about programming and tech engines hardware.
    The best thing about this post is the way you wrote it and the language you used. It was of high quality but yet accessible and easily understandable.
    I enjoyed reading and commenting on your post as much as I enjoyed playing your clever and entertaining game.
    I look forward to see your finished product, and wish you the best of luck and success with your future work.

    Mohamad Saleh

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s