feature/save_and_loaaaaaad #32
@@ -9,6 +9,15 @@ namespace Babushka.scripts.CSharp.Common.Savegame;
|
|||||||
[Serializable]
|
[Serializable]
|
||||||
public class SaveData
|
public class SaveData
|
||||||
{
|
{
|
||||||
|
// for future use in case the structure changes.
|
||||||
|
public const int VERSION = 1;
|
||||||
|
public int version { get; set; } = VERSION;
|
||||||
|
|
||||||
|
public bool IsVersionValid()
|
||||||
|
{
|
||||||
|
return VERSION == version;
|
||||||
|
}
|
||||||
|
|
||||||
public string SceneName;
|
public string SceneName;
|
||||||
public string Id;
|
public string Id;
|
||||||
public string JsonPayload;
|
public string JsonPayload;
|
||||||
|
|||||||
@@ -7,12 +7,15 @@ namespace Babushka.scripts.CSharp.Common.Savegame;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class SaveGameManager : Node
|
public partial class SaveGameManager : Node
|
||||||
{
|
{
|
||||||
|
public static string USER_DATA_FILE_PATH = "user://save_data/userData.json";
|
||||||
|
|
||||||
public static SaveGameManager? Instance { get; private set; } = null!;
|
public static SaveGameManager? Instance { get; private set; } = null!;
|
||||||
|
|
||||||
public override void _EnterTree()
|
public override void _EnterTree()
|
||||||
{
|
{
|
||||||
Instance = this;
|
Instance = this;
|
||||||
|
USER_DATA_FILE_PATH = ProjectSettings.GlobalizePath(USER_DATA_FILE_PATH);
|
||||||
|
SavegameService.SavePath = USER_DATA_FILE_PATH;
|
||||||
SavegameService.Load();
|
SavegameService.Load();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,10 +9,11 @@ namespace Babushka.scripts.CSharp.Common.Savegame;
|
|||||||
/// Handles the saving and loading of game states.
|
/// Handles the saving and loading of game states.
|
||||||
/// Holds the central SaveData object that serves as a temporary SaveFile.
|
/// Holds the central SaveData object that serves as a temporary SaveFile.
|
||||||
/// Automatically writes to disk on every scene change.
|
/// 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>
|
/// </summary>
|
||||||
public static class SavegameService
|
public static class SavegameService
|
||||||
{
|
{
|
||||||
public static readonly string SavePath = "user://babushka_savegame.json";
|
public static string SavePath = "";
|
||||||
|
|
||||||
public static Dictionary<string, string> SaveDatas = new ();
|
public static Dictionary<string, string> SaveDatas = new ();
|
||||||
|
|
||||||
@@ -72,9 +73,21 @@ public static class SavegameService
|
|||||||
public static void Save()
|
public static void Save()
|
||||||
{
|
{
|
||||||
string json = Json.Stringify(SaveDatas, indent: "\t");
|
string json = Json.Stringify(SaveDatas, indent: "\t");
|
||||||
|
|
||||||
|
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);
|
using var file = FileAccess.Open(SavePath, FileAccess.ModeFlags.Write);
|
||||||
file.StoreString(json);
|
file.StoreString(json);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
GD.Print(e.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Loads the current savegame file from disk and parses it into the SaveData dictionary.
|
/// Loads the current savegame file from disk and parses it into the SaveData dictionary.
|
||||||
@@ -83,8 +96,15 @@ public static class SavegameService
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (!System.IO.File.Exists(SavePath))
|
||||||
|
{
|
||||||
|
SaveDatas = new();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
string saveDataJson = FileAccess.GetFileAsString(SavePath);
|
string saveDataJson = FileAccess.GetFileAsString(SavePath);
|
||||||
SaveDatas = Json.ParseString(saveDataJson).AsGodotDictionary<string, string>();
|
SaveDatas = Json.ParseString(saveDataJson).AsGodotDictionary<string, string>();
|
||||||
|
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch(Exception e)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user