diff --git a/scripts/CSharp/Common/Inventory/InventoryInstance.cs b/scripts/CSharp/Common/Inventory/InventoryInstance.cs index 3ec2ba0..5945ff7 100644 --- a/scripts/CSharp/Common/Inventory/InventoryInstance.cs +++ b/scripts/CSharp/Common/Inventory/InventoryInstance.cs @@ -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(savePayload[0]); + int _amount = int.Parse(savePayload[1]); + + ItemInstance instance = new ItemInstance { blueprint = resource, amount = _amount }; + AddItem(instance); } } } diff --git a/scripts/CSharp/Common/Inventory/InventoryManager.cs b/scripts/CSharp/Common/Inventory/InventoryManager.cs index 86778da..36166b1 100644 --- a/scripts/CSharp/Common/Inventory/InventoryManager.cs +++ b/scripts/CSharp/Common/Inventory/InventoryManager.cs @@ -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(