Tool to edit .bgeconf file?

Hi everyone.
I’m making a game that uses globalDict.
It is not essential, but it could be very useful for me if there is a way to edit the .bgeconf file that globalDict generates, outside the game engine.
The file looks like this:


It would be useful for testing things while devolping the game.

Is there a way to edit the file?
Thanks

Edit: between the non-human-readable characters you can see some human-readable strings but when I change those the bge can’t read the file anymore… So that way doesn’t work

For you it looks better than me(I don’t know why replacing default UTF-8 to any larger range doesn’t work in Gedit when it can’t read file correctly. I am not sure if text editor can read it!

I can open the file with Notepad++, and I can edit the ‘human readable’ parts of the file, but then the file becomes corrupted, so I was curious if there is a way to edit it, in a way that the bge still can read it.

If you want to have a human-readable save file, you should save and load the file yourself. I would recommend using the json module, Though if you want to be able to save values that aren’t Python basic types, you’ll have to use something else like pickle.

The bgeconf files are saved using the Python built-in marshalling functions, which means they’re not designed to be human readable.
If you really want to use bgeconf (rather than just using an equally-as-easy to use format like JSON (which is also human readable)), then here’s some tools to write/read bgeconf files to/from json files (which are editable text files)




from os import path    
import json, marshal


def bgeconf_to_json(directory, filename):
    bgeconf_path = path.join(directory, "{}.bgeconf".format(filename))
    json_path = path.join(directory, "{}.json".format(filename))
    
    with open(bgeconf_path, "rb") as conf_file, open(json_path, "w") as json_file:
        return json.dump(marshal.load(conf_file), json_file)
    


def json_to_bgeconf(directory, filename):
    bgeconf_path = path.join(directory, "{}.bgeconf".format(filename))
    json_path = path.join(directory, "{}.json".format(filename))
    
    with open(bgeconf_path, "wb") as conf_file, open(json_path, "r") as json_file:
        return marshal.dump(json.load(json_file), conf_file)

Or, write JSON converters for such types, which is a PITA :wink:

Ok I can have a look at it tomorrow when I have time for it. But is it easy? I mean, is it worth it for just only change some things for some testing?

Yes, it is easy to use. Here’s a simple example:

import bge
import json

globDict = bge.logic.globalDict

globDict[1] = 'a'
globDict[2] = 'b'
globDict[3] = 'c'

# saving
with open('saveFileName.txt', 'w') as fd:
    json.dump(globDict, fd, indent=4)
    # Note: the indent argument is optional
    # but it makes the file fomatting a bit nicer

# loading
with open('saveFileName.txt', 'r') as fd:
    loadedDict = json.load(fd)
bge.logic.globalDict = loadedDict

Ok thanks I’ll try it tomorrow

cont = logic.getCurrentController()
    if cont.sensors['S'].positive:
        logic.loadGlobalDict
        gD = logic.globalDict
        with open('globalDictJson.txt', 'w') as fd:
            json.dump(gD, fd, indent = 4)
            print('hello')

It saves nothing, but there are also no errors printed in the console and it prints ‘hello’.
Help?

Check your working directory. I think Blender defaults to its installed directory unless you start Blender by opening a saved file.

It’s writing, as Mobious says, to the Blender directory, as you’ve not provided a path

Ow, ok, I found the file, but this is the file:

{}

And since I loaded globalDict first before I saved it I should expect some more than ‘{}’

Check your script. It doesn’t look like you actually load the dictionary, you just reference the loadGlobalDict method without calling it.

Oh, thanks, I forgot to type () behind logic.loadGlobalDict,
It works now, thanks for all your help

Ehh wait a minute,
What did I do wrong, it doesn’t change the globalDict when I changed the json file thing:

    cont = logic.getCurrentController()
    if cont.sensors['L'].positive:
        logic.loadGlobalDict()
        with open('globalDictJson.txt', 'r') as fd:
            loadedDict = json.load(fd)
        logic.globalDict = loadedDict
        logic.saveGlobalDict()

You’re probably keeping a reference to globalDict, which is now the old dictionary (as in your extract, you’re assigning the dict, not updating it). To update the dictionary, replace “logic.globalDict = loadedDict” with “logic.globalDict.update(loadedDict)”

ehh, I’m really really sorry, I just forgot to connect the sensor to the python controller… sorry lol.
logic.globalDict = loadedDict does work but your method may work as well.