How to modify serialized item attrubute value from save data?

Park

New member
Hi, I'm trying to modify item attribute's overried value from serialized save data to migrate data from previous version's data. This is my code.

C#:
if (Opsive.UltimateInventorySystem.SaveSystem.SaveData data.TryGetValue(InventorySystemManagerItemSaveKey, out var itemSaverSerialization))
{
    var itemSavedData = (InventorySystemManagerItemSaver.ItemsSaveData)itemSaverSerialization.DeserializeFields(MemberVisibility.All);
    var index = Array.FindLastIndex(itemSavedData.Items, x => x.ID == searchingItemID);

    itemSavedData.Items[index].ItemAttributeCollection.Deserialize(true);
    var attr = itemSavedData.Items[index].GetAttribute<Attribute<T>>(attributeName, false, false);
   
    if (attr != null)
    {
        attr.SetOverrideValue(callback.Invoke(attr.GetValue()));
        itemSavedData.Items[index].ItemAttributeCollection.Serialize();
       
        itemSaverSerialization.Serialize(itemSavedData, true, MemberVisibility.All);
        _inventorySaveData.Set(InventorySystemManagerItemSaveKey, itemSaverSerialization);
    }
}

When I debugged my code, 'SetOverrideValue' method executed successfully but an error occurred while executing 'Serialize'. It was stack overflow exception and There is one part that is suspected of causing problems.
I modified Opsive.UltimateInventorySystem.Core.Item class's ItemAttributeCollection property from internal to public to get attributeCollection in my migration code and added uint property 'ItemDefinitionID' to get ItemDefinitionID from saved data. Did these changes cause problems? If not, Is there any way to do what I want to do?

UIS version : 1.2.4
Unity Editor Version : 2020.3.40f1
Error Log :
1683019128851.png
 
I'm not sure I completely follow what's going on.
But a stack overflow most often comes from an infinite loop somwhere.

My guess is that since you made ItemCollection public it becomes serializable, which creates the recursive serialization. Item -> ItemCollection -> Item list -> Item -> repeat.

If you need the ItemCollection to be public such that you can access it, make a getter and or setter, don't make the field itself public.
 
I solved the problem. I modified MemberVisibility from all to public and then the error was gone. Thanks for your advice.
 
Top