title: Pickle load not working in certain situations
body: <p>I'm trying to make a autosave feature for my game, but in certain situations the load function doesn't work and I couldn't find the reason.</p>
  
  <p>game.py:</p>
  
  <pre><code>import pygame, sys, os, pickle
  pygame.init()
  
  pygame.mouse.set_visible(False)
  
  def save():
      os.remove('savefile.dat')
      with open('savefile.dat', 'wb') as f:
      pickle.dump([in_room.in_room1, in_room.in_room2, var.room_x, var.room_y], f, protocol = 2)
  
  def load():
      with open('savefile.dat', 'rb') as f:
          in_room.in_room1, in_room.in_room2, var.room_x, var.room_y = 
  pickle.load(f)
  
  class in_room():
      import menu
      in_menu = True
      in_room1 = False
      in_room2 = False
  
  class settings():
      res_x = 1280
      res_y = 720
      FPS = 30
  
  class win_setup():
      window = pygame.display.set_mode((settings.res_x, settings.res_y), pygame.FULLSCREEN)
      pygame.display.set_caption('The Old House 0.1')
  
  class var():
      location = os.path.dirname(os.path.realpath(__file__))
      black = (0, 0, 0)
      d_gray = (75, 75, 75)
      l_gray = (125, 125, 125)
      white = (255, 255, 255)
      myfont = pygame.font.SysFont('monospace', 20)
      room_x = 515
      room_y = 185
      room_x_def = 515
      room_y_def = 185
      fpsClock = pygame.time.Clock()
  
  class spr:
      logo = pygame.image.load(os.path.join(var.location, 'graphics/frostic_logo.png'))
      player = pygame.image.load(os.path.join(var.location, 'graphics/player_shadows.png'))
      player = pygame.transform.scale(player, (settings.res_x, settings.res_x)).convert_alpha()
      cursor = pygame.image.load(os.path.join(var.location, 'graphics/cursor.png')).convert_alpha()
      cursor = pygame.transform.scale2x(cursor)
      menu_bg = pygame.image.load(os.path.join(var.location, 'graphics/menu_bg.png'))
      menu_bg = pygame.transform.scale2x(menu_bg)
      newgame_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_newgame1.png'))
      continue_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_continue1.png'))
      exit_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_exit1.png'))
  
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              pygame.QUIT()
              sys.exit
  </code></pre>
  
  <p>menu.py:</p>
  
  <pre><code>import pygame, imp, sys, os
  import game
  from game import in_room, settings, win_setup, var, spr
  pygame.init()
  
  sel_item = 0
  
  while in_room.in_menu:
  
      # Selection:
  
      if sel_item == 0:
          spr.newgame_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_newgame2.png'))
          spr.continue_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_continue1.png'))
          spr.exit_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_exit1.png'))
      elif sel_item == 1:
          spr.newgame_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_newgame1.png'))
          spr.continue_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_continue2.png'))
          spr.exit_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_exit1.png'))
      elif sel_item == 2:
          spr.newgame_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_newgame1.png'))
          spr.continue_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_continue1.png'))
          spr.exit_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_exit2.png'))
  
      newgame_button = pygame.transform.scale2x(spr.newgame_texture)
      newgame_rect = newgame_button.get_rect(center = (640, 321))
      continue_button = pygame.transform.scale2x(spr.continue_texture)
      continue_rect = continue_button.get_rect(center = (640, 429))
      exit_button = pygame.transform.scale2x(spr.exit_texture)
      exit_rect = exit_button.get_rect(center = (640, 537))
  
      for event in pygame.event.get():
          if event.type == pygame.KEYDOWN:
              if event.key == pygame.K_w and sel_item &gt; 0:
                  sel_item -= 1
              elif event.key == pygame.K_s and sel_item &lt; 2:
                  sel_item += 1
              elif event.key == pygame.K_RETURN:
                  if sel_item == 0:
                      in_room.in_room1 = True
                      in_room.in_menu = False
                      var.room_x = var.room_x_def
                      var.room_y = var.room_y_def
                      import room1
                      imp.reload(room1)
                  elif sel_item == 1:
                      game.load()
                      if in_room.in_room1 == True:
                          import room1
                          imp.reload(room1)
                      elif in_room.in_room2 == True:
                          import room2
                          imp.reload(room2)
                  elif sel_item == 2:
                      pygame.quit()
                      sys.exit()
          elif event.type == pygame.QUIT:
              pygame.QUIT()
              sys.exit()
  
      win_setup.window.fill(var.black)
      win_setup.window.blit(spr.menu_bg, (0, 0))
      win_setup.window.blit(newgame_button, newgame_rect)
      win_setup.window.blit(continue_button, continue_rect)
      win_setup.window.blit(exit_button, exit_rect)
      pygame.display.update()
      var.fpsClock.tick(settings.FPS)
  </code></pre>
  
  <p>room1.py:</p>
  
  <pre><code>import pygame, imp, sys, os, math, pickle
  import game
  from game import in_room, settings, win_setup, var, spr
  pygame.init()
  
  last_saved = pygame.time.get_ticks()
  
  def pos_x(room_x):
      display_pos = var.myfont.render('X: ' + str(var.room_x), 1, var.white)
      win_setup.window.blit(display_pos, (0, 0))
  
  def pos_y(room_y):
      display_pos = var.myfont.render('Y: ' + str(var.room_y), 1, var.white)
      win_setup.window.blit(display_pos, (0, 20))
  
  def is_room1(in_room1):
      display_room = var.myfont.render('Is in room1: ' + str(in_room.in_room1), 1, var.white)
      win_setup.window.blit(display_room, (0, 40))
  
  def is_room2(in_room2):
      display_room = var.myfont.render('Is in room2: ' + str(in_room.in_room2), 1, var.white)
      win_setup.window.blit(display_room, (0, 60))
  
  while in_room.in_room1:
  
      room = pygame.image.load(os.path.join(var.location, 'graphics/room1_bg.jpg'))
  
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              pygame.QUIT()
              sys.exit
  
      # Rotation:
      mouse_pos = pygame.mouse.get_pos()
      angle = 270 - math.atan2(mouse_pos[1] - 360, mouse_pos[0] - 640) * 180 / math.pi
      player = pygame.transform.rotate(spr.player, angle).convert_alpha()
      rect = player.get_rect(center = (640, 360))
  
      # Movement:
      if pygame.key.get_pressed()[pygame.K_w]:
          var.room_y += 5
      elif pygame.key.get_pressed()[pygame.K_s]:
          var.room_y -= 5
      elif pygame.key.get_pressed()[pygame.K_d]:
          var.room_x -= 5
      elif pygame.key.get_pressed()[pygame.K_a]:
          var.room_x += 5
      elif pygame.key.get_pressed()[pygame.K_ESCAPE]:
          in_room.in_menu = True
          in_room.in_room1 = False
          import menu
          imp.reload(menu)
  
      # Collision
      if var.room_y &lt; 125:
          var.room_y += 5
      elif var.room_y &gt; 335 and var.room_x &gt; 530:
          var.room_y -= 5
      elif var.room_y &gt; 335 and var.room_x &lt; 495:
          var.room_y -= 5
      elif var.room_x &lt; 405:
          var.room_x += 5
      elif var.room_x &gt; 625:
          var.room_x -= 5
      elif var.room_y &lt; 125:
          var.room_y += 5
      elif var.room_y &gt; 335 and var.room_x &gt; 530:
          var.room_y -= 5
      elif var.room_y &gt; 335 and var.room_x &lt; 495:
          var.room_y -= 5
      elif var.room_x &lt; 405:
          var.room_x += 5
      elif var.room_x &gt; 625:
          var.room_x -= 5
  
      # Door to next room:
      if var.room_y &gt; 345 and var.room_x &lt; 530 and var.room_x &gt; 495:
          in_room.in_room2 = True
          in_room.in_room1 = False
          var.room_y = 130
          import room2
          imp.reload(room2)
  
      # Autosave:
      now = pygame.time.get_ticks()
  
      if now - last_saved &gt;= 1000:
          game.save()
          last_saved = now
  
      win_setup.window.fill(var.black)
      win_setup.window.blit(room, (var.room_x, var.room_y))
      win_setup.window.blit(player, rect)
      pos_x(var.room_x)
      pos_y(var.room_y)
      is_room1(in_room.in_room1)
      is_room2(in_room.in_room2)
      win_setup.window.blit(spr.cursor, mouse_pos)
      pygame.display.update()
      var.fpsClock.tick(settings.FPS)
  </code></pre>
  
  <p>I added the (almost) whole code so there wouldn't be any vital information missing. File room2.py is basically the same as room1.py so I didn't add that.</p>
  
  <p>There's propably already an answer to this problem but I couldn't find it.</p>
  
  <p>Also if you notice something else I could've done better, feel free to say it.</p>