added saving and loading support to collectable inventory items

This commit is contained in:
2025-11-25 17:14:30 +01:00
parent 70383fc16e
commit f37f7c7ceb
3 changed files with 37 additions and 9 deletions
@@ -63,6 +63,8 @@ public partial class ItemOnGround2D : Node, ISaveable
{
QueueFree();
}
UpdateSaveData();
}
}
@@ -92,7 +94,14 @@ public partial class ItemOnGround2D : Node, ISaveable
public void UpdateSaveData()
{
// do nothing?
var payloadData = new Dictionary<string, Variant>
{
{"pickupCounter", pickUpCounter}
};
string id = GetMeta("SaveID").AsString();
GD.Print($"Updating Save ID for object {Name}: {id}");
SavegameService.AppendDataToSave( id, payloadData);
}
public void LoadFromSaveData()
@@ -100,6 +109,26 @@ public partial class ItemOnGround2D : Node, ISaveable
if (_infiniteSupply)
return;
// standard check: how many times has this item been collected?
string id = GetMeta("SaveID").AsString();
GD.Print($"Loading Save ID for object {Name}: " + id);
Dictionary<string, Variant> save = SavegameService.GetSaveData(id);
if (save.Count > 0)
{
if(save.TryGetValue("pickupCounter", out Variant countVar))
{
int count = countVar.AsInt32();
GD.Print($"Item count for {Name}: {count}");
for (int i = 0; i < count; i++)
{
Pickup();
GD.Print($"Picking up {Name}: {i}");
}
}
}
//separate check for unique items: If already in inventory, delete this instance.
ItemResource itemResource = itemInstance.blueprint;
Dictionary<string, Variant> savegameData = SavegameService.GetSaveData(InventoryInstance.ID);
if (savegameData.Count > 0)
@@ -121,7 +150,6 @@ public partial class ItemOnGround2D : Node, ISaveable
}
}
}
}
}
}