0

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

Community
  • 1
  • 1
BiBo
  • 11
  • 4
  • Have you tried `import dill; dill.dump_session()`? (Although, I agree with @holdenweb as to why things might be acting unexpectedly). – Mike McKerns Jul 03 '15 at 13:17

1 Answers1

0

Your problem isn't pickle. Your problem is the assumption that restoring the state of a room from a pickle is sufficient to have it operate normally when you load the pickle. I'd be willing to bet you have no tests to validate that assumption.

When you save (pickle) an instance, the values of its instance variables are saved as part of the pickle. When you reload them their __init__() methods will not normally be called, which is presumably how actions are started. So are you sure that there's enough state information in the pickle for things to just resume where they left off?

The easiest way to save all the rooms without having to list them individually is to have a list containing the rooms. Then your code would refer to (say) rooms[100] rather than room100, and you can just pickle the rooms[] list to save them all with a single statement.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • thanks @holdenweb. What would be the best way to test for that? I have tried printing out the values of the variables and they seem to be changing (for example, the x position of the bees that I mentioned before was changing appropriately, just for some reason the actual sprites werent moving to where the data was saying they were supposed to be). I had tried the list once before but it didnt work at the time. I wonder why? Also as for dill, I apparently dont have the dill module so importing it doesnt work. – BiBo Jul 05 '15 at 15:28
  • "I wonder why" is always a good question when evidence is presented about which one can hypothesize. But without seeing the code you wrote it would be very difficult to say why using a list "didn't work at the time.". It's not like lists are known to work badly on Fridays ... As to testing, try sprinkling some `assert` statements or `print` calls in your code to determine what happens when it's loaded from a pickle. You need to understand the state of your rooms. – holdenweb Jul 06 '15 at 21:33