I’ve been working on a project recently and have been using Scriptable Objects to create a unique and dynamic item system. However, I’ve run into a bit of a snag when it comes to managing item durability, specifically with instances of the same item in the inventory. I’m hoping some of you might have insights into solving this issue.
Here’s a bit about what I’ve done so far: I’ve implemented a loot system where items are instantiated based on drop rates. This part seems to be working fine; I loop over the number of items in the drop and instantiate the respective item objects. These items are then stored in the inventory.
The problem I’m facing arises when I try to handle the durability of these items, particularly when there are multiple instances of the same item in the inventory. For instance, let’s say I have 5 Hunting Bows in the inventory. If I equip one of them and use it, the durability should reduce for that specific equipped bow, not affect the durability of all the other bows in the inventory.
I’ve noticed that the way I’ve set up my system seems to be causing this issue. When I decrease the durability for the equipped bow, it’s affecting all the other instances of that item as well. Clearly, I need a way to make each item instance unique so that their properties, like durability, are independent of each other.
I’m wondering if any of you have encountered a similar situation or have insights into how to handle this effectively. Perhaps there’s a way to modify my approach to ensure that each item instance maintains its own properties and state. I’m also open to considering different techniques or design patterns that might work better for this scenario.
If you’ve successfully dealt with a situation like this or have any suggestions on how to make items unique while maintaining shared characteristics. The code below was my last try to a solution
for (int i = 0; i < drop.number; i++)
{
if (drop.item is WeaponConfig && i > 0)
{
LogInfo("Inside drop.item is WeaponConfig condition");
var droppedItem = drop.item as WeaponConfig;
if (droppedItem == null)
{
LogError("droppedItem is null. Skipping.");
return;
}
LogInfo("Creating new item instance...");
var item = ScriptableObject.CreateInstance<WeaponConfig>();
var repairRequirements = ScriptableObject.CreateInstance<RepairRequirements>();
LogInfo("Setting item properties...");
item.ItemID = System.Guid.NewGuid().ToString();
item.name = droppedItem.GetDisplayName();
item.RepairRequirements = repairRequirements;
item.Category = droppedItem.Category;
item.ConsumableCategory = droppedItem.ConsumableCategory;
item.Icon = droppedItem.Icon;
item.Price = droppedItem.Price;
item.Definition = droppedItem.Definition;
item.Category = droppedItem.Category;
// Set the repair requirements properties
if (droppedItem.RepairRequirements == null)
{
LogError("droppedItem.RepairRequirements is null. Skipping.");
return;
}
LogInfo("Setting repair requirements properties...");
item.RepairRequirements.ItemRepairRequirements = droppedItem.RepairRequirements.ItemRepairRequirements;
item.RepairRequirements.IsItemDamaged = droppedItem.RepairRequirements.IsItemDamaged;
item.RepairRequirements.ItemDurability = droppedItem.RepairRequirements.ItemDurability;
item.RepairRequirements.RepairAmount = droppedItem.RepairRequirements.RepairAmount;
LogInfo("Adding item to inventory...");
inventory.AddToFirstEmptySlot(item, 1);
}
else
{
LogInfo("Inside else condition");
inventory.AddToFirstEmptySlot(drop.item, 1);
}
}