✨ Implemented Loading function on fields and plants
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using FileAccess = Godot.FileAccess;
|
||||
@@ -8,7 +9,10 @@ public static class SavegameService
|
||||
{
|
||||
public static readonly string SavePath = "res://savegame/savegame.json";
|
||||
|
||||
public static Dictionary<string, Variant> SaveDatas = new ();
|
||||
public static Dictionary<string, string> SaveDatas = new ();
|
||||
|
||||
public static bool _loaded = false;
|
||||
|
||||
|
||||
public static void AppendSave(SaveData saveData)
|
||||
{
|
||||
@@ -23,12 +27,49 @@ public static class SavegameService
|
||||
SaveDatas.Add(key, saveData.JsonPayload);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static string GetSaveData(string sceneName, string id)
|
||||
{
|
||||
string saveData = "";
|
||||
|
||||
string key = string.Concat(sceneName, "_", id);
|
||||
|
||||
if (!_loaded)
|
||||
{
|
||||
GD.Print("SavegameService: SaveFile not loaded.");
|
||||
return saveData;
|
||||
}
|
||||
|
||||
if (SaveDatas.ContainsKey(key))
|
||||
{
|
||||
saveData = SaveDatas[key];
|
||||
}
|
||||
|
||||
return saveData;
|
||||
}
|
||||
|
||||
|
||||
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($"Game saved to {file}");
|
||||
}
|
||||
|
||||
public static void Load()
|
||||
{
|
||||
try
|
||||
{
|
||||
string saveDataJson = FileAccess.GetFileAsString(SavePath);
|
||||
SaveDatas = Json.ParseString(saveDataJson).AsGodotDictionary<string, string>();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
GD.PrintRich(e.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
_loaded = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user