Duplicate Dictionary for game saving

I’m using the Pickle module to save game data (dictionaries) to save files for a game I’m working on. These dictionaries contain KX_GameObjects which can’t be saved by Pickle, so I have to sort through the dictionaries and replace these objects with None values.

This is the code I’m using:

# g.blocks is the original dictionary of blocks
saveblocks = {}
    for i in g.blocks:
        data = g.blocks<i>
        saveblocks[i] = data
    for i in saveblocks:
        saveblocks[i][3] = None

However, [I]any changes to the saveblocks dictionary also affects g.blocks as well, meaning that the code I used didn’t duplicate the dictionary data as I thought it would. This basically breaks the game after using the save function.

I tried using deepcopy, but it can’t process the KX_GameObject class either.

Does anyone have an idea of another way to duplicate dictionary data (not using dict.copy), preferably without having to pack additional external python modules along with the .blend?

I found one working solution in my case:

saveblocks = {}
    for i in g.blocks:
        data = g.blocks[i]
        saveblocks[i] = [data[0], data[1], data[2], None, data[4], data[5]]

It solves my specific problem, but it would still be nice to know if there were a way to duplicate dictionaries in all cases where simply cutting around the problem can’t be done as I did.

I suggest to simply not place references to “temporary” objects in the dictionary to be stored. If you need them place them in another dictionary. From there you can reference the data in the first dictionary if necessary.

Finally you got a dictionary for saving and a dictionary for working. Both can have it’s specific structure, as each one has a specific purpose.

Otherwise, you might need to go through every single item of the dictionary, removing the references as you do already.

use copy.deepcopy this will create a dictionary without rereferencing to the root.
I had the same Problem: copyung dict A to dict B, changes on B were done also in A, what it should not do…
google for deepcopy.