How do I make a item system with ScriptableObjects where different items can have durability?
Okay so I have a ScriptableObject item system (items can be consumables, weapons, etc), The item ScriptableObject contains data like itemType, icon, damage, durability etc... I have inventory slot system where the slots store the items by a reference of their respective ScriptableObject asset.. The problem is, imagine I have 2 pickaxes in two different slots and each of them should have their durability modified independently when i use them but if I change the slot item reference durability the ScriptableObject asset gets changed. Currently the way im getting around this is every time I add an item to slot, I create a instance of the item ScriptableObject and setting all the values of the item asset to that instance and add that instance to that slot instead of the item asset it self.. This method is very bad because im making redundant copies of the scriptable objects.
Heres an example of what I have :
//This function adds the provided item to a slot instead of passing in the actual item i pass in the instance
public void AddItem(Item item)
{
Item inst = CreateItemInstance(item);
AddToInventory(inst);
}
public static Item CreateItemInstance(Item item)
{
Item instance = ScriptableObject.CreateInstance(typeof(Item));
instance.name = item.name; instance.type = item.type; instance.itemPrefab = item.itemPrefab; instance.itemGrabber = item.itemGrabber; instance.endurance = item.endurance; instance.damage = item.damage; instance.healthConsumeAmount = item.healthConsumeAmount; instance.energyConsumeAmount = item.energyConsumeAmount; instance.icon = item.icon; instance.maxItemsPerSlot = item.maxItemsPerSlot; instance.description = item.description; return instance;
}
Answer by ojjnartheking · Oct 07, 2021 at 11:36 AM
Hello! I actually made a post just like yours. You can see it here:
Unfortunately I don't have the answer for this, but someone replied to my post that may or may not come in handy.