feature/save_and_loaaaaaad #32
@@ -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()
|
||||
{
|
||||
var sceneName = _sceneKeyProvider.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)
|
||||
{
|
||||
|
||||
@@ -17,8 +17,7 @@ public partial class InventoryInstance : Node, ISaveable
|
||||
|
||||
[Signal]
|
||||
public delegate void InventoryContentsChangedEventHandler();
|
||||
|
||||
private const string SCENE_NAME = "inventory";
|
||||
|
||||
private const string ID = "instace";
|
||||
|
||||
/// <summary>
|
||||
@@ -192,15 +191,14 @@ public partial class InventoryInstance : Node, ISaveable
|
||||
}
|
||||
}
|
||||
|
||||
SavegameService.AppendDataToSave(SCENE_NAME, ID, payloadData);
|
||||
SavegameService.AppendDataToSave(ID, payloadData);
|
||||
}
|
||||
|
||||
public void LoadFromSaveData()
|
||||
{
|
||||
var sceneName = SCENE_NAME;
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -18,12 +18,11 @@ public class SaveData
|
||||
return VERSION == version;
|
||||
}
|
||||
|
||||
public string SceneName;
|
||||
public string Id;
|
||||
public string JsonPayload;
|
||||
|
||||
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 void AppendDataToSave(string scenename, string id, Dictionary<string, Variant> payload)
|
||||
public static void AppendDataToSave( string id, Dictionary<string, Variant> payload)
|
||||
{
|
||||
var saveData = new SaveData();
|
||||
|
||||
saveData.SceneName = scenename;
|
||||
saveData.Id = id;
|
||||
saveData.JsonPayload = Json.Stringify(payload, indent: "\t");
|
||||
AppendSave(saveData);
|
||||
@@ -36,15 +34,13 @@ public static class SavegameService
|
||||
/// <param name="saveData"></param>
|
||||
private static void AppendSave(SaveData saveData)
|
||||
{
|
||||
string key = string.Concat(saveData.SceneName, "_", saveData.Id);
|
||||
|
||||
if (SaveDatas.TryGetValue(key, out var value))
|
||||
if (SaveDatas.TryGetValue(saveData.Id, out var value))
|
||||
{
|
||||
SaveDatas[key] = saveData.JsonPayload;
|
||||
SaveDatas[saveData.Id] = saveData.JsonPayload;
|
||||
}
|
||||
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="id"></param>
|
||||
/// <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();
|
||||
|
||||
string key = string.Concat(sceneName, "_", id);
|
||||
string saveDataString = "";
|
||||
|
||||
if (!_loaded)
|
||||
@@ -69,9 +64,9 @@ public static class SavegameService
|
||||
return saveData;
|
||||
}
|
||||
|
||||
if (SaveDatas.ContainsKey(key))
|
||||
if (SaveDatas.ContainsKey(id))
|
||||
{
|
||||
saveDataString = SaveDatas[key];
|
||||
saveDataString = SaveDatas[id];
|
||||
saveData = Json.ParseString(saveDataString).AsGodotDictionary<string, Variant>();
|
||||
}
|
||||
|
||||
@@ -88,8 +83,10 @@ public static class SavegameService
|
||||
|
||||
GD.Print(SavePath);
|
||||
// create cloud directory
|
||||
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(SavePath) ?? "");
|
||||
|
||||
CreateSaveDirectory();
|
||||
GD.Print("Save.");
|
||||
|
||||
try
|
||||
{
|
||||
using var file = FileAccess.Open(SavePath, FileAccess.ModeFlags.Write);
|
||||
@@ -101,21 +98,28 @@ public static class SavegameService
|
||||
}
|
||||
}
|
||||
|
||||
private static void CreateSaveDirectory()
|
||||
{
|
||||
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(SavePath) ?? "");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the current savegame file from disk and parses it into the SaveData dictionary.
|
||||
/// </summary>
|
||||
public static void Load()
|
||||
{
|
||||
GD.Print("Load.");
|
||||
try
|
||||
{
|
||||
if (!System.IO.File.Exists(SavePath))
|
||||
{
|
||||
SaveDatas = new();
|
||||
return;
|
||||
CreateSaveDirectory();
|
||||
}
|
||||
|
||||
string saveDataJson = FileAccess.GetFileAsString(SavePath);
|
||||
SaveDatas = Json.ParseString(saveDataJson).AsGodotDictionary<string, string>();
|
||||
if(!string.IsNullOrEmpty(saveDataJson))
|
||||
SaveDatas = Json.ParseString(saveDataJson).AsGodotDictionary<string, string>();
|
||||
|
||||
}
|
||||
catch(Exception e)
|
||||
|
||||
@@ -11,10 +11,6 @@ namespace Babushka.scripts.CSharp.Common.Temp;
|
||||
/// </summary>
|
||||
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")]
|
||||
[Export] private Node2D _penTarget;
|
||||
[Export] private int _transferDelayMs;
|
||||
@@ -70,7 +66,7 @@ public partial class MVPDuck : Node2D, ISaveable
|
||||
|
||||
string id = GetMeta("SaveID").AsString();
|
||||
GD.Print($"Updating Save ID for object {Name}: {id}");
|
||||
SavegameService.AppendDataToSave(_sceneKeyProvider.Payload.AsString(), id, payloadData);
|
||||
SavegameService.AppendDataToSave( id, payloadData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -78,11 +74,10 @@ public partial class MVPDuck : Node2D, ISaveable
|
||||
/// </summary>
|
||||
public void LoadFromSaveData()
|
||||
{
|
||||
var sceneName = _sceneKeyProvider.Payload.AsString();
|
||||
string id = GetMeta("SaveID").AsString();
|
||||
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)
|
||||
{
|
||||
float xPos = 0;
|
||||
|
||||
Reference in New Issue
Block a user