🚧 WIP implementing save and load for the inventory

pull/32/head
kziolkowski 2 months ago
parent b205e45096
commit 72079044bd

@ -3,6 +3,9 @@ using System;
using Godot;
using System.Collections.Generic;
using System.Linq;
using Babushka.scripts.CSharp.Common.Farming;
using Babushka.scripts.CSharp.Common.Savegame;
using Godot.Collections;
namespace Babushka.scripts.CSharp.Common.Inventory;
@ -17,6 +20,9 @@ public partial class InventoryInstance : Node
[Signal]
public delegate void InventoryContentsChangedEventHandler();
private const string SCENE_NAME = "inventory";
private const string ID = "instace";
/// <summary>
/// The total amount of Inventoryslots in the inventory (empty and occupied).
/// </summary>
@ -156,4 +162,51 @@ public partial class InventoryInstance : Node
{
return items.All(HasItems);
}
#region SAVE AND LOAD
public void UpdateSaveData()
{
var saveData = new SaveData();
saveData.SceneName = SCENE_NAME;
saveData.Id = ID;
var payloadData = new Godot.Collections.Dictionary<string, Variant>();
for (int i = 0; i < _slots.Count; i++)
{
if (!_slots[i].IsEmpty())
{
string key = i.ToString();
string[] value = new string[2];
value[0] = _slots[i].itemInstance.blueprint.ResourcePath;
value[1] = _slots[i].itemInstance.amount.ToString();
payloadData.Add(key,value);
}
}
saveData.JsonPayload = Json.Stringify(payloadData, indent: "\t");
SavegameService.AppendSave(saveData);
}
public void LoadFromSaveData()
{
var sceneName = SCENE_NAME;
var id = ID;
string jsonPayload = SavegameService.GetSaveData(sceneName, id);
Godot.Collections.Dictionary<string, Variant> save = Json.ParseString(jsonPayload).AsGodotDictionary<string, Variant>();
if (save.Count > 0)
{
for (int i = 0; i < _slots.Count; i++)
{
if (save.TryGetValue(i.ToString(), out Variant inventoryItem))
{
// todo: we have the resource paths and amounts, but how do we get to ItemResources and ItemInstances?
}
}
}
}
#endregion
}

Loading…
Cancel
Save