You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Babushka/scripts/CSharp/Low Code/Variables/SaveableVariableNode.cs

64 lines
1.6 KiB

using Babushka.scripts.CSharp.Common.Inventory;
using Babushka.scripts.CSharp.Common.Savegame;
using Godot;
using Godot.Collections;
namespace Babushka.scripts.CSharp.Low_Code.Variables;
public partial class SaveableVariableNode : VariableNode, ISaveable
{
[Export] private bool _debug;
public override void _EnterTree()
{
LoadFromSaveData();
ValueChanged += UpdateSaveData;
SavegameService.OnSaveGameReset += SaveGameReset;
}
private void SaveGameReset()
{
Payload = default;
GD.Print($"Saveable Variable reset to {Payload}");
}
public override void _ExitTree()
{
ValueChanged -= UpdateSaveData;
SavegameService.OnSaveGameReset -= SaveGameReset;
}
public void UpdateSaveData()
{
var payloadData = new Dictionary<string, Variant>
{
{ "payload", Payload },
};
string id = GetMeta("SaveID").AsString();
SavegameService.AppendDataToSave( id, payloadData);
}
public void LoadFromSaveData()
{
string id = GetMeta("SaveID").AsString();
Dictionary<string, Variant> save = SavegameService.GetSaveData(id);
if (save.Count > 0)
{
if (Payload.VariantType == Variant.Type.Int)
{
Payload = save["payload"].AsInt32();
}
else
{
Payload = save["payload"];
}
if (_debug)
{
GD.Print($"SaveableVariable {Name} loaded payload: {Payload}.");
}
}
}
}