- Home /
Problem to add a delete method to a saveload script C#
I have this SaveLoad code and i'm trying to make a delete funtion, tried to use SaveLoad.SavedGames.remove = game.current; but not worked, can someone help me? The script just create a file with player names, i know how to delete the file, but i want to delete just one player, by this i mean, just one string from the saved file. here is my code: using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Runtime.Serialization.Formatters.Binary; using System.IO;
public static class SaveLoad {
public static List<Game> savedGames = new List<Game>();
//it's static so we can call it from anywhere
public static void Save() {
SaveLoad.savedGames.Add(Game.current);
BinaryFormatter bf = new BinaryFormatter();
//Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located
FileStream file = File.Create (Application.persistentDataPath + "/savedGames.gd"); //you can call it anything you want
bf.Serialize(file, SaveLoad.savedGames);
file.Close();
}
public static void Load() {
if(File.Exists(Application.persistentDataPath + "/savedGames.gd")) {
Debug.Log(Application.persistentDataPath);
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/savedGames.gd", FileMode.Open);
SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
file.Close();
}
}
}
Hi,
Thanks for sharing your SaveLoad method. Have you any idea how can I the savedGames.gd to www?
Answer by leandroreschke · May 27, 2014 at 09:16 PM
Already solved my problem ^_^, i openwrite the file, delete just the game.current(current Player) and save it again. The problem with playerprefs is that if i want the player to export your data with cloud or to other device, it's a problem for me.Here is my new code if anyone need! using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Runtime.Serialization.Formatters.Binary; using System.IO;
public static class SaveLoad {
public static List<Game> savedGames = new List<Game>();
//it's static so we can call it from anywhere
public static void Save() {
SaveLoad.savedGames.Add(Game.current);
BinaryFormatter bf = new BinaryFormatter();
//Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located
FileStream file = File.Create (Application.persistentDataPath + "/savedGames.gd"); //you can call it anything you want
bf.Serialize(file, SaveLoad.savedGames);
file.Close();
}
public static void Load() {
if(File.Exists(Application.persistentDataPath + "/savedGames.gd")) {
Debug.Log(Application.persistentDataPath);
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/savedGames.gd", FileMode.Open);
SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
file.Close();
}
}
public static void Delete() {
SaveLoad.savedGames.Remove(Game.current);
BinaryFormatter bf = new BinaryFormatter();
//Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located
FileStream file = File.OpenWrite(Application.persistentDataPath + "/savedGames.gd"); //you can call it anything you want
bf.Serialize(file, SaveLoad.savedGames);
file.Close();
}
}
Hello! thanks for posting the new code, there is something i don't understand tho: you made a list for savegames and i can see it adds the current savegame as new save but what happens when there already is a savefile, you are writing it to the same path under the same name so won't it overwrite it or if it doesn't overwrite how would i let the player choose which saved game to load?
Answer by Good-old-Grim · May 27, 2014 at 07:22 AM
Overwrite the file. Editing it in place is more trouble than its worth.
Answer by fafase · May 27, 2014 at 07:30 AM
Is it really worth implementing your own system? Unity offers the PlayerPrefs class which stores info in the deep folder into registry which makes hacking slightly harder (just a slight bit).
Even though it only stores a limited amount of type (int, float, string), you could create your own parsing method to store it all into a string and parse it back into whatever type you need.
Then PlayerPrefs has a delete method that will erase all info.
Obviously, if you are after a learning process this does not apply.
Then you would have to load the file in a temporary string (or StringBuilder) then look for the starting index (index 1) of the object you are after and the ending index (index 2) of the line using IndexOf method.
Use substring to cut the string in two strings (start to index 1, index 2 to end) and join those two together, overwrite the file with the new file.
I don't like storing data on the registry, it just seems unnecessary. not top mention registry cleaners from antivirus like AVG may wipe the save data (registry cleaners).
when you have about 120+ variables being stored. (each number of enemies killed, the stats of 13 characters(14 variables per character). registry storage is virtually out the window.
what i do is use an XOR encryption and save it to a folder in User/documents/CompanyName or whatever yet still moderately secure well far more that of the registry. still fast enough to auto-save very often.
this also allows for multiple saves. Playerprefs, from its name and description in the docs sound likes its more of a config file than a save system.