"ArgumentException: An item with the same key has already been added" When loading a save

desukarhu

New member
Hello,

Continuation from discord.

So my in my database I basically only have item templates that turn into randomized items at runtime. "Sword" ItemDefinition could turn into "Copper Sword of Brawn" at runtime and so forth. There are some consumable items and crafting materials that are not mutable. But all my equipment is.

These are my item definition attributes:
itemdefinitionattribtues.jpg

Item attributes:

itemattributes.jpg

The only custom class I have there as you can see is the ShinyStats ModifierElement. It's basically just a data wrapper that tells how to modify my players stats:

modifierelement.jpg

What I've noticed for certain is that the more items I have in my Inventory the higher chance for the error to occur. With a full inventory (I have set a limit of 40 items) it happens like 80% of time. But it doesn't happen every time for certain so it's quite difficult to test. But I'm fairly certain it's the mutable equipment that is causing it.

I also attached JSON of a save file my PixelCrushers save system created. In this save file the duplicate key is "-1951305544"
 

Sangemdoko

Moderator
Staff member
Hi desukarhu

I've had a look at your save data and it does seem that the InventorySystemManagerItemSaver serialized save data has a dupplicated hashValue. I've been discussing with Justin and we're not sure how that could happen. We're convinced that an item can be saving itself twice, so perhaps it's just a coincidence that the hash value is duplicate.
My guess is that you have many ModifierElement attribute values, and since hashvalues are computed using the type name of a field + a previous hash, my guess is that you have a higher likelyhood of getting two equal hash values than other users.

Could you send your project to support@opsive.com? Make sure to specify this post in the email. Then Justin can have a look at it in detail and see if he can replicate the issue and find a fix.
 

desukarhu

New member
Hi desukarhu

I've had a look at your save data and it does seem that the InventorySystemManagerItemSaver serialized save data has a dupplicated hashValue. I've been discussing with Justin and we're not sure how that could happen. We're convinced that an item can be saving itself twice, so perhaps it's just a coincidence that the hash value is duplicate.
My guess is that you have many ModifierElement attribute values, and since hashvalues are computed using the type name of a field + a previous hash, my guess is that you have a higher likelyhood of getting two equal hash values than other users.

Could you send your project to support@opsive.com? Make sure to specify this post in the email. Then Justin can have a look at it in detail and see if he can replicate the issue and find a fix.
Ah damn, I haven't even added all of my stats as attributes yet. When I have all of them I would have double the amount of ModifierElements.
I don't necessarily need to use the ModifierElement class though, I could just use a string list attribute that would have enties like: "Strength: '+2%'" and "Agility: '+10'" and then parse the string in my Equipper. Do you think that would work better and avoid the duplicates? Or what if my ModifierElements were in a list?

Do you have any tips how to send projects? I have third party assets that I don't think I'm allowed to redistribute and my project size is 5 gigs at least.

Edit: It does indeed seem like changing the modifiers to a list of strings indeed fixed it, at least for now. But I'm just worried there's still a chance for it to happen again randomly at some point in the game, because the error completely wiped the players inventory.
 
Last edited:

Sangemdoko

Moderator
Staff member
We are looking into the issue but we are still unsure how there could be a dupplicate hash value.
Having access to your project would really help us narrow it down.

If possible do you think you could dupplicate your project and strip it down to only include your inventory database and character with a small scene that reproduces the error? You can package it up, add it to a Google or One Drive and send us a download link to support@opsive.com.
 

desukarhu

New member
We are looking into the issue but we are still unsure how there could be a dupplicate hash value.
Having access to your project would really help us narrow it down.

If possible do you think you could dupplicate your project and strip it down to only include your inventory database and character with a small scene that reproduces the error? You can package it up, add it to a Google or One Drive and send us a download link to support@opsive.com.
I'll try to find time to make one, it'll be quite a bit of work. Especially if it somehow magically doesn't happen anymore in a new project.
 

desukarhu

New member
We are looking into the issue but we are still unsure how there could be a dupplicate hash value.
Having access to your project would really help us narrow it down.

If possible do you think you could dupplicate your project and strip it down to only include your inventory database and character with a small scene that reproduces the error? You can package it up, add it to a Google or One Drive and send us a download link to support@opsive.com.
I've been working on this with no success. In a new project and using the UIS Demo I couldn't replicate the error even after adding many ModifierElement item attributes and generating a lot of random items at runtime (500 items) using the same methods I use in my main project. Very annoying! I don't really know what else to try.

In my project I first generate the items and add them to enemy/chest inventory, then when the item drop conditions happen I instantiate loot label prefabs on the ground around the enemy corpse/opened chest, when the player clicks on the label the item is given (using the GiveItem function) from the enemy/chest inventory to the players inventory. Do you think the error could happen here somehow?

I already changed my main project to use a list of strings to represent stat modifiers and it has worked well so far with no errors. But again, it's quite scary knowing the error has a chance of happening and ruining the players save file.
 

Sangemdoko

Moderator
Staff member
The error happens within the save component that saves all the items in the game.
In your game do you only save the player inventory? or do you save other inventories/ itemobject states?

I know it is frustrating, but unless we can reproduce this error we cannot know if any changes we make fixed it.
If nothing else work I would recommend you send us your full project.

The other thing I could suggest is adding some code around the moment you save that can detect an error and if it does abort the save such to not override the previous uncorrupted save data. That would also be useful in case someone quits your game while it is saving.

I'm very sorry I cannot help you more on this issue
 

desukarhu

New member
The error happens within the save component that saves all the items in the game.
In your game do you only save the player inventory? or do you save other inventories/ itemobject states?

I know it is frustrating, but unless we can reproduce this error we cannot know if any changes we make fixed it.
If nothing else work I would recommend you send us your full project.

The other thing I could suggest is adding some code around the moment you save that can detect an error and if it does abort the save such to not override the previous uncorrupted save data. That would also be useful in case someone quits your game while it is saving.

I'm very sorry I cannot help you more on this issue
I sadly don't have time to debug this further. But not using the ModifierElement class as an attribute fixed it for me and I don't get the error anymore.
 
Top