- Home /
Is it a good idea to save game data in a multidimentional array?
In a racing game, where each car has lots of variables to keep track of what parts are installed,(for example; selectedWheels, selectedSpoiler, selectedColdAirIntake, selectedExhaustSystem etc.) is it a good idea to store these variables in a multidimentional array and then just save the whole array to the save file?
let's say I have 12 cars in the game and each car has 10 different part categories, when all part variables is 0, it means all the parts are stock.
int[,] carParts= new int[12, 10] {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
Hi, it depends how you save data and how future-proof you want to be. Imagine you make a version 1.1 of the game with 11 car parts, then you may have trouble reading a v1.0 savegame... Saving game as X$$anonymous$$L or other text file format might be a good idea to get more flexibility. You can still compress it or use simple encryption to prevent players to cheat too easily (it can always be done but sometimes plain text is too easy ;-)
I don't know your constraints, targeted platforms, size restriction etc... but a little flexibility is always a good idea ;-)
I'd like to add the multi-dimensional arrays can't be serialized, so you will have to flatten it first.
Edit: Not true, thanks to Baste for pointing out my error.
@Cherno If we're talking about writing to file, that's not an issue. I'd assume that the save would be loaded from file at play, and written to file during play, so you never run into Unity's serialization.
I wasn't talking about Unity's own serialization, I meant plain .NET serialization. Now that you question it though, I researched it and it looks like it's actually possible to serialzie multi-dimensional arrays. Heh. Somehow I always though it wouldn't work. To think my whole life was a lie... Nooo! :D
Thanks for correcting my error.
Answer by Baste · Aug 14, 2015 at 03:26 PM
That's not very extensible! What if you want to introduce a new part category, or add more cars? You old save games just broke, and you have to re-write the save system code.
Which is a bunch of work, and each re-write might introduce new bugs. That's always fun!
I would pick up one of the standard ways of serializing C# classes to file, and use that to create a text-representation of your cars (or whatever you're actually serializing), and saving that. The most-used format out there is JSON, so you'll find plenty of tutorials for it. You can look at what it does here.
Another alternative is YAML, which is what Unity uses internally. There's some prebuilt libraries out there that you can work with.
Finally, you could create your own save-file format. I'd suggest not having a random array, but write some proper format that's both easy to read and easy to parse. And, most importantly, don't rely on the order of the information in the file, as that'll make expanding the format a lot harder to do. You want it to look something like this:
Car:
name: coolCar
selectedWheels: 15
selectedSpoiler: 12
...
Car:
name: otherCar
selectedWheels: 5
selectedSpoiler: 7
...
It's pretty easy to parse - split the input by the string "Car:", and read every line by splitting over the character ":". This is very close to both JSON and YAML, so you'd kinda be reinventing the wheel, but it might be easier to work with your own solution instead of grabbing some library. Up to you.
When you get close to release, and you don't want users to be able to edit the save file, you could run it through some kind of hashing function to prevent that.
Thanks, good points. I'll have to do some research to completely understand everything you said;) Currenly I use the save system described in this video: https://www.youtube.com/watch?v=yxziv4ISfys
Your answer
![](https://koobas.hobune.stream/wayback/20220611235851im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to stop the car after the finish line? 2 Answers
Drifting code not 100% accurate? 1 Answer
car steering wheel rotation? 0 Answers
Nitro bar setup 1 Answer
RotateAround() using local rotation axes 2 Answers