feature/save_and_loaaaaaad #32

Merged
kziolkowski merged 18 commits from feature/save_and_loaaaaaad into develop 2025-11-26 14:41:29 +01:00
2 changed files with 23 additions and 5 deletions
Showing only changes of commit 2a4425b9db - Show all commits
@@ -3,9 +3,7 @@ using System;
using Godot; using Godot;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Babushka.scripts.CSharp.Common.Farming;
using Babushka.scripts.CSharp.Common.Savegame; using Babushka.scripts.CSharp.Common.Savegame;
using Godot.Collections;
namespace Babushka.scripts.CSharp.Common.Inventory; 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) public InventoryActionResult AddItem(ItemInstance newItem)
{ {
var result = AddItemAndStackRecursive(newItem, 0); var result = AddItemAndStackRecursive(newItem, 0);
@@ -201,9 +212,14 @@ public partial class InventoryInstance : Node
{ {
for (int i = 0; i < _slots.Count; i++) 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; private int _currentSelectedSlotIndex = 0;
@@ -34,7 +34,9 @@ public partial class InventoryManager : Node
public override void _Ready() public override void _Ready()
{ {
playerInventory = new InventoryInstance();
playerInventory.SlotAmount = 37; playerInventory.SlotAmount = 37;
AddChild(playerInventory);
} }
public InventoryActionResult CreateItem( public InventoryActionResult CreateItem(