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)
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
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.