Making more than one save slot in the globalDict - is it possible?

Is it possible to create more than 1 save file using the globalDict? In many older games, it was possible to have 3 save slots. So obviously, how would one go about implementing a system where you can “split” the globalDict into three seperate save slots, and how to copy/delete them while the game is running?

I am not meaning to make more than 1 bgeconf file here, the file is one bgeconf file, but has two or more “save slots” where the save data is written to, while configuration, options etc stays global.

GD = logic.globalDict

slot_1 = GD['save_slot_1']
slot_2 = GD['save_slot_2']
slot_3 = GD['save_slot_3']
#etc

#removing:
GD['save_slot_1'] = ''
#or
del GD['save_slot_1']

How do I map this to an integer

the variable just use 1 instead of slot_1, as for the GD[‘’] don’t think this is possible due to the GD stores a string of data.

but you can always use a split() function to remove the unneeded slot_ part. and use int() to convert the string number into an int.

The way I want this to work is this:

  1. The user clicks on a “save”.

  2. That save is selected by a script (save can be named whatever you want)

  3. The script loads everything thats in the save (options stay global across all files).

  4. Should no data be found in a save, the game asks you to create a new save. This writes data to the saves respective slot. The game will not ask you to create a new save on starting, instead it is upon clicking the empty file.

  5. If the user wishes to, they can ‘erase’ the save by clicking the Delete button, which brings up a prompt confirming deletion. This “deletion” actually resets the save by setting it to ‘’ or using del in Python.

How can this all be put into Python?

There is also a problem with Collection actuators. Try adding an overlay collection, and it will either not find the camera you want it to be (must be same camera as same collection) or it will say that the “collection is not found”. Even if you get past all these, the added collection may be invisible and not work. I think this may be a bug.

For 0.3 i can not say, best use here is to use bpy coding for it.

You need to look into the OS module. It has a check and directory scan.

could be helpfull:

I actually have a system like this working in my game (the script is a bit complicated so I’ll just put some modified snippets)

Basically, I just get the save data path

def getFullPath(string):
    path = bge.logic.expandPath(string)
    return path


def getSaveDir():
    return getFullPath("//saves\\")

and then use JSON to save the data into a file

def saveGame(obj, saveFile):

    if "playerData" not in obj:
        obj["playerData"] = {}

    if "globalTime" not in obj:
        obj["globalTime"] = 2.00


    writeSave = {
        "playerData": obj["playerData"],
        "globalTime": obj["globalTime"]
    }

    jsonData = json.dumps(writeSave)
    with open(saveFile, 'w') as outfile:
        outfile.write(jsonData)
        # I think you can also do json.dump(writeSave, outfile)
        # I had to modify my script a lil since I use the orjson lib in my game

For save slots, I would use multiple different files so like slot_1.txt, slot_2.txt, …
Then to load from a specific slot you can just append the number to the name

# Load game data into data object from save file
def loadGameData(obj):
    saveFile = f"{getSaveDir()}slot_{obj['saveSlot']}.txt"

    if os.path.isfile(saveFile):
        with open(saveFile, 'r') as openfile:
            data = json.loads(openfile.read())
            for key in data:
                obj[key] = data[key]

    # If no save file exists at that location, make a default one
    else:
        saveGame(obj, saveFile)
        loadGameData(obj)

The problem with this setup, is that I want to be able to store variables inside the save slots (my save slots are named file_1, file_2, file_3).

With a custom module, I can return the save file number for an active save slot when the user clicks on a save file object with an integer property. This is done by assigning the integer property (1 to 3) to its respective slot which is used to retrieve the save file. Now the issue is to load from/save to it. I know you would need to convert the save list to a dictionary since they are now a list stored inside a dictionary.

How can I convert the save slot(s) so I can save to/load from the save slots? Can you please help me on how to convert the save slot so it is a dictionary rather than a list so that I can use items() on it?

For example, I would like to load/save data from slot 1, but you cannot load from a list, it must be converted into a dictionary for use with globalDict.

Sorry if I was slightly confusing here. Edited to be more clear.

A simple google search would be enough: