Global Dictionary Editing with python question

As of right now my game has a periodical auto-save system. However, this system makes it hard to introduce enemies and player deaths. So I want to modify this into a snapshot system. I want to make a script that copies a dictionary, and then stores it for later use. So when the player dies, the game loads the copy, not the most recent save, as that would load them moments before they died, likely causing them to endlessly die on that save.

This snapshot system would be much more stable than directly loading auto-save data. I like that you can close the game, then re-open it to being exactly where you left off, but what if your game glitched and you’re outside the map? Well, in that case you could manually load the game where you decided a safe place to save the game was.

In short, I want to know how I can duplicate a dictionary in the global dict. Right now, I have an options dictionary, and a progress dictionary. I would like to copy the progress dictionary, and then save it into a snapshot dictionary. That would leave me with three dictionaries inside the global dict. The snapshot being loaded, instead of the progress dictionary.

you can look in my sig for the save and load blend.
in there i save a dict to a file and location of choice, also loading it again.

I want to copy a dictionary that is already in the global dictionary.

that can be done that way.

you got your GD, you then save it to a file with the code in my blend and load it with the other code part to use it.
your autosave still saves it to your GD, so you are making a copy this way.

I don’t want to use a different saving system to make a copy. Do you know how I can duplicate an already existing dictionary and save it as a new dictionary? I want to use this duplicate as a backup for when the player dies, so the auto-save data isn’t loaded.

I think what you are looking for is:


...
bge.logic.globalDict['snapShotDictionary'] = bge.logic.globalDict['progressDictionary'].copy()
...

To check that any dictionary exists inside globalDict (you may need to check whether certain dictionaries
exist in globalDict before referencing them):


if 'anyDictionary' in globalDict:
    [..do stuff..]

Thank you. Would issuing the same code again overwrite the data? Such as at a later time in the game, when the snapshot is saved again, would the newer data overwrite the older data?

Also, can I search for a specific dictionary, rather than “anyDictionary”?

Thank you. Would issuing the same code again overwrite the data? Such as at a later time in the game, when the snapshot is saved again, would the newer data overwrite the older data?

You’re welcome! And yes it would, which means if you want to have other snapshot data, you would have to have other dictionaries/other methods for storage.

Also, can I search for a specific dictionary, rather than “anyDictionary”?

Yes.

ok what do you really want?

save a dict
{‘blabla’: value, ‘blabla2’: value2}

into the globaldict(savegame) as an copy?
the use mirrors way

or really just make a copy of your existing savegame?

so using a piece of code from my script as i meant for you, not the whole save system.


def write_save_file(GD):


    os.makedirs(path, exist_ok=True)


    file = open(path + file_name, 'w')


    file.write(str(GD))
    file.close()

so if you feed it your GD
it will copy it to a file and location of choice.

#edit
oh boy im to tired.

i thought you wanted to save a dict to a new file.
it can be GD[someinfo] or just your complete GD

if you use the GD alot make it easier for your self and do something like this:

GD = bge.logic.globalDict

saves you quite some text to write.

then just do:

GD['dict you want to copy to'] = GD['the dict data']

bge.logic.globalDict[‘snapShotDictionary’] = bge.logic.globalDict[‘progressDictionary’].copy()

Oh boy, I believe I have made a mistake.

Please DO NOT copy in these ways unless you really intend to (where a and b are dictionaries to copy from/to):


a = b.copy()
a = b

Otherwise, you could run into some real problems.

A better way is (for your situation) to deep copy the dictionaries like so (where a and b are dictionaries to copy from/to):


import copy
a = copy.deepcopy(b)

What happens with the former ways is you get shallow references to the dictionaries, meaning if you change one dictionary, the other will change too.

I agree with Mirror|rorriM. You need deepcopy() otherwise you get a copied list, but the items refer to exactly the same objects (dependent on the type of the item).

Thanks guys. I think this will work. I’ll reply again if I have another question. :slight_smile: