file reading problems

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\”, line 1394, in loads
return Unpickler(file).load()
File “E:\Programs\Python\Lib\”, line 872, in load
KeyError: ‘4’

I would have done a search, but honestly, I have no idea what to search for.

the.blend is here:
and the level file:

I would be greatly pleased if someone were to explain what’s wrong

If people want to test with different data, the syntax for the level file is:

number of objects in the level
size of floor
object name
object position
object name
object position

the floor sizes are 1(10x10), 2(20x20), or 3(40x40)
the objects are currently: HWall (horizontal wall section) and VWall (vertical wall section)


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
### 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

### Close save file

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.