Reworked existing save system for user data: Made path steam-compatible, added Versioning and try/catch to save
This commit is contained in:
@@ -9,10 +9,11 @@ namespace Babushka.scripts.CSharp.Common.Savegame;
|
||||
/// Handles the saving and loading of game states.
|
||||
/// Holds the central SaveData object that serves as a temporary SaveFile.
|
||||
/// Automatically writes to disk on every scene change.
|
||||
/// Uses the Godot Json Serializer, which may implicitly convert integers to floats, so be careful when extending!
|
||||
/// </summary>
|
||||
public static class SavegameService
|
||||
{
|
||||
public static readonly string SavePath = "user://babushka_savegame.json";
|
||||
public static string SavePath = "";
|
||||
|
||||
public static Dictionary<string, string> SaveDatas = new ();
|
||||
|
||||
@@ -72,8 +73,20 @@ public static class SavegameService
|
||||
public static void Save()
|
||||
{
|
||||
string json = Json.Stringify(SaveDatas, indent: "\t");
|
||||
using var file = FileAccess.Open(SavePath, FileAccess.ModeFlags.Write);
|
||||
file.StoreString(json);
|
||||
|
||||
GD.Print(SavePath);
|
||||
// create cloud directory
|
||||
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(SavePath) ?? "");
|
||||
|
||||
try
|
||||
{
|
||||
using var file = FileAccess.Open(SavePath, FileAccess.ModeFlags.Write);
|
||||
file.StoreString(json);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
GD.Print(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -83,8 +96,15 @@ public static class SavegameService
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!System.IO.File.Exists(SavePath))
|
||||
{
|
||||
SaveDatas = new();
|
||||
return;
|
||||
}
|
||||
|
||||
string saveDataJson = FileAccess.GetFileAsString(SavePath);
|
||||
SaveDatas = Json.ParseString(saveDataJson).AsGodotDictionary<string, string>();
|
||||
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user