I have been looking everywhere for information on pickle, and I thought I had had figured it out, but I am having a bit of a problem getting it to work properly.
So I use python 3.3.1. I have a pygame game with many rooms (around 200), each room is its own child instance of a parent class, many with their own variables to keep track of all sorts of things that happen in their room. As the player goes from room to room, I of course want the changes the player does to each room to be saved when the player saves a game. I have sort of a double question.
So here is more or less what I do:
import pickle
with gzip.GzipFile("Saved Games/"+file_name, 'wb') as output:
pickle.dump(room001, output, pickle.HIGHEST_PROTOCOL)
...
pickle.dump(room200, output, pickle.HIGHEST_PROTOCOL)
pickle.dump(player, output, pickle.HIGHEST_PROTOCOL)
So I guess that saves. But when I load it later, using:
with gzip.GzipFile("Saved Games/"+file_name, 'r') as loadfile:
room001 = pickle.load(loadfile)
...
room200 = pickle.load(loadfile)
player = pickle.load(loadfile)
at first everything seems okay. It seems to load with no problems, but then I notice the game is acting a little strangely. it is as if it is not using the data it loaded from the pickle.
For example, one room might have some bees flying about. I enter the room and see the bees as normal, but when the event function for the room tries to make the bees move about, they don't. It would look something like this:
def room001_events():
room001.bee_pos_x += room001.bee_speed
if room001.bee_pos_x < 0 or room001.bee_pos_x > 600:
room001.bee_speed *= -1
Prior to the load the bees would happily bounce back and forth, but after it, they wouldnt move at all. So I am wondering what I am doing wrong with the load process. the function is being accessed and when I print out the values, they seem to be changing, but the bees on the screen don't move.
Also, I am wondering if there is some way to iterate over all the rooms in the program without having to list them individually. I have tried using shelve as per this question, but it just crashes on load (it mostly seems to have problems with surfaces from what I can tell because it keeps giving me pygame.error: display Surface quit even though I never called pygame.quit() at all).