♻️ reworked and debugged the SaveSystem. Removed unnecessary scene reference.
This commit is contained in:
@@ -202,15 +202,14 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
SavegameService.AppendDataToSave(_sceneKeyProvider.Payload.AsString(), SaveId + _fieldIndex.Payload.AsString(), payloadData);
|
SavegameService.AppendDataToSave(SaveId + _fieldIndex.Payload.AsString(), payloadData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LoadFromSaveData()
|
public void LoadFromSaveData()
|
||||||
{
|
{
|
||||||
var sceneName = _sceneKeyProvider.Payload.AsString();
|
|
||||||
var id = SaveId + _fieldIndex.Payload.AsString();
|
var id = SaveId + _fieldIndex.Payload.AsString();
|
||||||
|
|
||||||
Dictionary<string, Variant> save = SavegameService.GetSaveData(sceneName, id);
|
Dictionary<string, Variant> save = SavegameService.GetSaveData(id);
|
||||||
|
|
||||||
if (save.Count > 0)
|
if (save.Count > 0)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ public partial class InventoryInstance : Node, ISaveable
|
|||||||
[Signal]
|
[Signal]
|
||||||
public delegate void InventoryContentsChangedEventHandler();
|
public delegate void InventoryContentsChangedEventHandler();
|
||||||
|
|
||||||
private const string SCENE_NAME = "inventory";
|
|
||||||
private const string ID = "instace";
|
private const string ID = "instace";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -192,15 +191,14 @@ public partial class InventoryInstance : Node, ISaveable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SavegameService.AppendDataToSave(SCENE_NAME, ID, payloadData);
|
SavegameService.AppendDataToSave(ID, payloadData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LoadFromSaveData()
|
public void LoadFromSaveData()
|
||||||
{
|
{
|
||||||
var sceneName = SCENE_NAME;
|
|
||||||
var id = ID;
|
var id = ID;
|
||||||
|
|
||||||
Godot.Collections.Dictionary<string, Variant> save = SavegameService.GetSaveData(sceneName, id);
|
Godot.Collections.Dictionary<string, Variant> save = SavegameService.GetSaveData(id);
|
||||||
|
|
||||||
if (save.Count > 0)
|
if (save.Count > 0)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -18,12 +18,11 @@ public class SaveData
|
|||||||
return VERSION == version;
|
return VERSION == version;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string SceneName;
|
|
||||||
public string Id;
|
public string Id;
|
||||||
public string JsonPayload;
|
public string JsonPayload;
|
||||||
|
|
||||||
public string ToString()
|
public string ToString()
|
||||||
{
|
{
|
||||||
return SceneName + " " + Id + " " + JsonPayload;
|
return Id + " " + JsonPayload;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,11 +20,9 @@ public static class SavegameService
|
|||||||
public static bool _loaded = false;
|
public static bool _loaded = false;
|
||||||
|
|
||||||
|
|
||||||
public static void AppendDataToSave(string scenename, string id, Dictionary<string, Variant> payload)
|
public static void AppendDataToSave( string id, Dictionary<string, Variant> payload)
|
||||||
{
|
{
|
||||||
var saveData = new SaveData();
|
var saveData = new SaveData();
|
||||||
|
|
||||||
saveData.SceneName = scenename;
|
|
||||||
saveData.Id = id;
|
saveData.Id = id;
|
||||||
saveData.JsonPayload = Json.Stringify(payload, indent: "\t");
|
saveData.JsonPayload = Json.Stringify(payload, indent: "\t");
|
||||||
AppendSave(saveData);
|
AppendSave(saveData);
|
||||||
@@ -36,15 +34,13 @@ public static class SavegameService
|
|||||||
/// <param name="saveData"></param>
|
/// <param name="saveData"></param>
|
||||||
private static void AppendSave(SaveData saveData)
|
private static void AppendSave(SaveData saveData)
|
||||||
{
|
{
|
||||||
string key = string.Concat(saveData.SceneName, "_", saveData.Id);
|
if (SaveDatas.TryGetValue(saveData.Id, out var value))
|
||||||
|
|
||||||
if (SaveDatas.TryGetValue(key, out var value))
|
|
||||||
{
|
{
|
||||||
SaveDatas[key] = saveData.JsonPayload;
|
SaveDatas[saveData.Id] = saveData.JsonPayload;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
SaveDatas.Add(key, saveData.JsonPayload);
|
SaveDatas.Add(saveData.Id, saveData.JsonPayload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,11 +52,10 @@ public static class SavegameService
|
|||||||
/// <param name="sceneName"></param>
|
/// <param name="sceneName"></param>
|
||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static Dictionary<string, Variant> GetSaveData(string sceneName, string id)
|
public static Dictionary<string, Variant> GetSaveData(string id)
|
||||||
{
|
{
|
||||||
Dictionary<string, Variant> saveData = new();
|
Dictionary<string, Variant> saveData = new();
|
||||||
|
|
||||||
string key = string.Concat(sceneName, "_", id);
|
|
||||||
string saveDataString = "";
|
string saveDataString = "";
|
||||||
|
|
||||||
if (!_loaded)
|
if (!_loaded)
|
||||||
@@ -69,9 +64,9 @@ public static class SavegameService
|
|||||||
return saveData;
|
return saveData;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SaveDatas.ContainsKey(key))
|
if (SaveDatas.ContainsKey(id))
|
||||||
{
|
{
|
||||||
saveDataString = SaveDatas[key];
|
saveDataString = SaveDatas[id];
|
||||||
saveData = Json.ParseString(saveDataString).AsGodotDictionary<string, Variant>();
|
saveData = Json.ParseString(saveDataString).AsGodotDictionary<string, Variant>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +83,9 @@ public static class SavegameService
|
|||||||
|
|
||||||
GD.Print(SavePath);
|
GD.Print(SavePath);
|
||||||
// create cloud directory
|
// create cloud directory
|
||||||
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(SavePath) ?? "");
|
|
||||||
|
CreateSaveDirectory();
|
||||||
|
GD.Print("Save.");
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -101,20 +98,27 @@ public static class SavegameService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void CreateSaveDirectory()
|
||||||
|
{
|
||||||
|
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(SavePath) ?? "");
|
||||||
|
}
|
||||||
|
|
||||||
/// <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.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Load()
|
public static void Load()
|
||||||
{
|
{
|
||||||
|
GD.Print("Load.");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!System.IO.File.Exists(SavePath))
|
if (!System.IO.File.Exists(SavePath))
|
||||||
{
|
{
|
||||||
SaveDatas = new();
|
SaveDatas = new();
|
||||||
return;
|
CreateSaveDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
string saveDataJson = FileAccess.GetFileAsString(SavePath);
|
string saveDataJson = FileAccess.GetFileAsString(SavePath);
|
||||||
|
if(!string.IsNullOrEmpty(saveDataJson))
|
||||||
SaveDatas = Json.ParseString(saveDataJson).AsGodotDictionary<string, string>();
|
SaveDatas = Json.ParseString(saveDataJson).AsGodotDictionary<string, string>();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,10 +11,6 @@ namespace Babushka.scripts.CSharp.Common.Temp;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class MVPDuck : Node2D, ISaveable
|
public partial class MVPDuck : Node2D, ISaveable
|
||||||
{
|
{
|
||||||
[ExportGroup("Persistence")]
|
|
||||||
// A container for the current scene. Used to automatically add the current scene context to the save ID.
|
|
||||||
[Export] public VariableResource _sceneKeyProvider;
|
|
||||||
|
|
||||||
[ExportGroup("Animation")]
|
[ExportGroup("Animation")]
|
||||||
[Export] private Node2D _penTarget;
|
[Export] private Node2D _penTarget;
|
||||||
[Export] private int _transferDelayMs;
|
[Export] private int _transferDelayMs;
|
||||||
@@ -70,7 +66,7 @@ public partial class MVPDuck : Node2D, ISaveable
|
|||||||
|
|
||||||
string id = GetMeta("SaveID").AsString();
|
string id = GetMeta("SaveID").AsString();
|
||||||
GD.Print($"Updating Save ID for object {Name}: {id}");
|
GD.Print($"Updating Save ID for object {Name}: {id}");
|
||||||
SavegameService.AppendDataToSave(_sceneKeyProvider.Payload.AsString(), id, payloadData);
|
SavegameService.AppendDataToSave( id, payloadData);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -78,11 +74,10 @@ public partial class MVPDuck : Node2D, ISaveable
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void LoadFromSaveData()
|
public void LoadFromSaveData()
|
||||||
{
|
{
|
||||||
var sceneName = _sceneKeyProvider.Payload.AsString();
|
|
||||||
string id = GetMeta("SaveID").AsString();
|
string id = GetMeta("SaveID").AsString();
|
||||||
GD.Print($"Loading Save ID for object {Name}: " + id);
|
GD.Print($"Loading Save ID for object {Name}: " + id);
|
||||||
|
|
||||||
Dictionary<string, Variant> save = SavegameService.GetSaveData(sceneName, id);
|
Dictionary<string, Variant> save = SavegameService.GetSaveData(id);
|
||||||
if (save.Count > 0)
|
if (save.Count > 0)
|
||||||
{
|
{
|
||||||
float xPos = 0;
|
float xPos = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user