Added Save and load support to the inventory

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

@ -3,9 +3,7 @@ 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;
@ -48,6 +46,19 @@ public partial class InventoryInstance : Node
}
}
public override void _EnterTree()
{
LoadFromSaveData();
InventoryContentsChanged += UpdateSaveData;
SlotAmountChanged += UpdateSaveData;
}
public override void _ExitTree()
{
InventoryContentsChanged -= UpdateSaveData;
SlotAmountChanged -= UpdateSaveData;
}
public InventoryActionResult AddItem(ItemInstance newItem)
{
var result = AddItemAndStackRecursive(newItem, 0);
@ -201,9 +212,14 @@ public partial class InventoryInstance : Node
{
for (int i = 0; i < _slots.Count; i++)
{
if (save.TryGetValue(i.ToString(), out Variant inventoryItem))
if (save.TryGetValue(i.ToString(), out Variant inventoryItemData))
{
// todo: we have the resource paths and amounts, but how do we get to ItemResources and ItemInstances?
string[] savePayload = inventoryItemData.AsStringArray();
ItemResource resource = ResourceLoader.Load<ItemResource>(savePayload[0]);
int _amount = int.Parse(savePayload[1]);
ItemInstance instance = new ItemInstance { blueprint = resource, amount = _amount };
AddItem(instance);
}
}
}

@ -23,7 +23,7 @@ public partial class InventoryManager : Node
}
}
public InventoryInstance playerInventory = new InventoryInstance();
public InventoryInstance? playerInventory;
private int _currentSelectedSlotIndex = 0;
@ -34,7 +34,9 @@ public partial class InventoryManager : Node
public override void _Ready()
{
playerInventory = new InventoryInstance();
playerInventory.SlotAmount = 37;
AddChild(playerInventory);
}
public InventoryActionResult CreateItem(

Loading…
Cancel
Save