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).
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:
- 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.
- 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  contains a number, element  contains a different number, and element  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.
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!