- Home /
Need ideas to save some values
Hi there I'm still learning while trying to make a little racing game where you can choose from between 12 cars and you are able to upgrade each car up to 3 levels or stars.
Now this is my question, I created a variable for a default acceleration and it get's multiplied times the current upgrade level, how can I save the upgrade level "for each car" do I have to create a variable for each car with somethin like car1LevelX or is there a way to take the other ones I already created like "int CurrentVehicle()" and "int CarAccel()" I'm thinking of a multidimensional array but I'm not sure if it's the way to go nor how to implement this to save this value later with a binary formatter.
Thanks to all!!!
Answer by MacDx · Oct 27, 2017 at 09:40 PM
IMO, serializing your data with a binary formatter is the way to go, so here are a couple of suggestions:
Define a dictionary of with strings as keys and ints as values (or whatever number type you use to define the current upgrade level). Give each car a name, then take these names and create a dictionary with them and serialize the dictionary. The first time you will create the dictionary with a default value, for every name and after that you will load the file with the data an get your dictionary from there.
A second option is to serialize an object graph instead. If you think that in the future you might want to save other kind of data for you cars you might as well define a couple of serializable classes which contain fields for the data you want to save.
I'll leave you with a code sample for the second option since it is the one I like the most :P
//GameData.cs
[System.Serializable]
public class GameData
{
public List<CarData> cars;
}
//CarData.cs
[System.Serializable]
public class CarData
{
public string name;
public int upgradeLevel;
}
//DataManager.cs
public class DataManager : MonoBehaviour
{
private GameData gameData;
void Start()
{
Load();
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
string path = Application.streamingAssetsPath + "/Save.data";
FileStream fs;
if(File.Exists(path))
fs = File.Open(path,System.IO.FileMode.Open, FileAccess.Write);
else
fs = File.Create(path);
bf.Serialize(fs,gameData);
fs.Close();
}
public void Load()
{
string path = Application.streamingAssetsPath + "/Save.data";
if(File.Exists(path))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = File.Open(path,FileMode.Open,FileAccess.Read);
gameData = (GameData) bf.Deserialize(fs);
fs.Close();
}
else
{
gameData = new GameData();
gameData.cars = new List<CarData>();
//Add the cars you want to this list based on the ones that are on your game
//Maybe something like
//CarData mustang = new CarData() {name = "mustang" , upgradeLevel = 1};
//gameData.cars.Add(mustang);
//And after you have finished creating the initial data call save to write the save file for the first time
Save();
}
}
}
And that's it. A simple serializing class. I hope this helps! :D
Edit: Note that I did not include a single using statement, I was a bit lazy with those but I'm sure you'll easily find and add the ones you need for this code
yeah this gives me some ideas I'll have to check them out, I appreciate the detail of the binary formatter I wasn't focusing that much in that part for the moment at least but tells me how to take it from there.
Right now I was doing something like
void $$anonymous$$yCarLevel()
{
int[,] currentCarLevel = new int[1, 2]{ {CurrentVehicle(),upgradeLevel} };
print("Your car is: " + currentCarLevel[0, 0] + " it's Upgrade Level: " + currentCarLevel[0, 1]);
}
I'm figuring out now how to save this array for each car, mostly I'm using switch to select the car, but still going to test your way