I’m writing a simple level creator, in which it reads from a file and places objects accordingly. The goings were smooth enough, I’m basically following Blendenzo’s tut (a really good one, BTW, thanks Blendenzo) until I ran into a problem, having something to do with the matrix I used for location in the level file.
The error return is
Traceback (most recent call last):
File “level builder”, line 24, in ?
File “E:\Programs\Python\Lib\pickle.py”, line 1394, in loads
return Unpickler(file).load()
File “E:\Programs\Python\Lib\pickle.py”, line 872, in load
dispatchkey
KeyError: ‘4’
I would have done a search, but honestly, I have no idea what to search for.
It’s a problem with the way I used the pickler in that tutorial. I’m not sure exactly how to use it, but I know that I used it wrong (I got the same error when I tried to make a save/load a few days ago for 10nas).
MolFlesH suggested using the eval() function instead. I haven’t tested it out, but here’s the example he gave:
### Add objects to be saved to this list ###
cube = GameLogic.getCurrentScene().getObjectList()["OBCube"]
### Open the file "Game1.sav" in write mode
saveFile = open("Game1.sav", "w")
### Write header to file
saveFile.write("This is a valid save file
")
### Write game data to file
saveFile.write(str(cube.getPosition()) + "
")
saveFile.write(str(cube.getOrientation()) + "
")
### Close save file
saveFile.close()
### Add objects to be loaded to this list ###
cube = GameLogic.getCurrentScene().getObjectList()["OBCube"]
### Open the file "Game1.sav" in read mode
loadFile = open("Game1.sav", "r")
### Check for the header
header = loadFile.readline()
header = header[0:-1]
if header == "This is a valid save file":
### Load game data from file
cube.setPosition(eval(loadFile.readline()))
cube.setOrientation(eval(loadFile.readline()))
### Close save file
loadFile.close()
I hope you can see the usage from the example. The other (less elegant) solution I’ve used is to split the location matrix into three separate strings, read them all back into a new matrix, then assign the new matrix.