Ticket #73: BasicSaveLoad.patch

File BasicSaveLoad.patch, 4.7 KB (added by rexiebaby, 10 years ago)

Game no longer crashes during save/load or map changes. NPC and PC position information are NOT saved.

  • engine.py

     
    5050        """Clears the data on a map reload so we don't have objects/npcs from 
    5151           other maps hanging around. 
    5252           @return: None""" 
    53  
     53         
    5454    def save(self, path, filename): 
    5555        """Writes the saver to a file. 
    5656           @type filename: string 
     
    8888                 
    8989        self.game_state.PC.behaviour = behaviour_player 
    9090 
    91     def load(self, filename): 
     91    def load(self, path, filename): 
    9292        """Loads a saver from a file. 
    9393           @type filename: string 
    9494           @param filename: the name of the file (including path) to load from 
    9595           @return: None""" 
    96          
     96 
    9797        # TODO 
    9898        # Remove what we are currently working with  
    9999        print "TODO: load" 
     
    101101        self.view.active_map.model.deleteMaps() 
    102102         
    103103        try: 
    104             f = open(filename, 'r') 
     104            f = open(path + filename, 'r') 
    105105        except(IOError): 
    106106            sys.stderr.write("Error: Can't find save game file\n") 
    107107            return 
    108108        self.game_state = pickle.load(f) 
    109109        f.close() 
    110110         
     111        print self.game_state.current_map_file 
     112 
    111113        # Make sure we're on the right map 
    112114        if self.game_state.current_map_file: 
    113115            self.loadMap(self.game_state.current_map_name, \ 
    114                          self.game_state.current_map_file)  
     116                         self.game_state.current_map_file) 
    115117 
    116118    def createObject (self, layer, attributes, instance): 
    117119        """Create an object and add it to the current map. 
     
    135137        else: 
    136138            self.addObject(layer, obj, instance) 
    137139 
    138          
    139  
    140140    def addPC(self, layer, pc, instance): 
    141141        """Add the PC to the map 
    142142           @type layer: fife.Layer 
     
    162162           @param instance: FIFE instance of object 
    163163           @return: None 
    164164        """ 
    165  
     165         
    166166        ref = self.game_state.getObjectById(obj.ID, \ 
    167167                                            self.game_state.current_map_name)  
    168168        if ref is None: 
    169169            # no, add it to the game state 
    170170            self.game_state.objects[self.game_state.current_map_name][obj.ID] = obj 
     171             
    171172        else: 
    172173            # yes, use the current game state data 
    173174            obj.X = ref.X 
    174175            obj.Y = ref.Y 
    175             obj.gfx = ref.gfx   
     176            obj.gfx = ref.gfx 
    176177              
    177178        if obj.trueAttr("NPC"): 
    178179            # create the agent 
    179180            obj.setup() 
    180              
     181         
    181182            # create the PC agent 
    182183            obj.start() 
    183184 
     
    261262        self.view.loadMap(map_name, str(map_file)) 
    262263        self.view.setActiveMap(map_name) 
    263264        self.reset() 
    264          
     265 
    265266        # If the map has been loaded, we might need to add some 
    266267        # agents to the PC and NPS 
     268         
    267269        for map_id in self.game_state.objects: 
    268270            for (object_id, npc) in self.game_state.objects[map_id].items(): 
    269271                if npc.trueAttr("NPC") and npc.behaviour == None: 
  • world.py

     
    9090        """Saves the game state, delegates call to engine.Engine 
    9191           @return: None""" 
    9292        self.data.save(*args, **kwargs) 
    93  
     93         
    9494    def loadGame(self, *args, **kwargs): 
    9595        """Loads the game state, delegates call to engine.Engine 
    9696           @return: None""" 
    97         self.data.load(*args, **kwargs) 
     97        self.data.load(*args,**kwargs) 
    9898         
    9999    def loadMap(self, map_name, filename): 
    100100        """Loads a map and stores it under the given name in the maps list. 
     
    104104           @param filename: File which contains the map to be loaded 
    105105           @return: None 
    106106        """ 
    107         if not map_name in self.maps: 
    108             """Need to set active map before we load it because the map  
    109             loader uses call backs that expect to find an active map.  
    110             This needs to be reworked. 
    111             """ 
    112             map = Map(self.engine, self.data) 
    113             self.maps[map_name] = map         
    114             self.setActiveMap(map_name) 
    115             map.load(filename) 
    116      
     107         
     108        """This portion need to be modified to prevent the default map xml from loading 
     109        PC and NPC positions as default.  Probably a new save and load scheme.""" 
     110        map = Map(self.engine, self.data) 
     111        self.maps[map_name] = map         
     112        self.setActiveMap(map_name) 
     113        map.load(filename) 
     114 
    117115    def setActiveMap(self, map_name): 
    118116        """Sets the active map that is to be rendered. 
    119117           @type map_name: text