Changeset 542


Ignore:
Timestamp:
06/17/10 10:23:33 (9 years ago)
Author:
beliar
Message:

Added engine attribute to DynamicObject? which can be used to access the engine.

Changed method to prepare a state for saving and restoring from a save. Only DynamicObject? has getstate and setstate now and those call prepareStateForSaving and restoreState respectively.

Every class that inherits from DynamicObject? - directly or indirectly - that needs the state prepared before being saved now only has to add the prepareStateForSaving method. In this method the following has to be done:

  1. Call prepareStateForSaving of the BaseClass? that is a subclass of DynamicObject?
  2. Change the state so that i can be saved (using pickle) - ie. by removing swig-objects - and restored - ie. adding values to the state from which attributes can be restored that are not automatically created when init is called.

Note that the state object that is passed to this method is only a copy of the actually state. Modifying this will not modify the actual class.

The restoreState function needs to do the following:

  1. Get any values added in prepareStateForSaving, restore attributes for which they where needed and remove this values from the state.
  2. Call restoreState pf the BaseClass? that is a subclass of DynamicObject? - this will restore all other attributes.

restoreState is only needed if values where added in prepareStateForSaving.

Location:
trunk/game/scripts/objects
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/game/scripts/objects/actors.py

    r541 r542  
    132132        self.createBehaviour(agent_layer) 
    133133     
    134     def __getstate__(self): 
    135         odict = self.__dict__.copy() 
    136         del odict["behaviour"] 
    137         return odict; 
    138      
    139     def __setstate__(self, state): 
    140         self.__dict__.update(state) 
     134    def prepareStateForSaving(self, state): 
     135        """Prepares state for saving 
     136        @type state: dictionary 
     137        @param state: State of the object   
     138        """ 
     139        GameObject.prepareStateForSaving(self, state) 
     140        del state["behaviour"] 
    141141 
    142142    def meet(self, npc): 
     
    298298        self.dialogue = kwargs.get('dialogue') 
    299299 
    300     def __getstate__(self): 
    301         odict = self.__dict__.copy() 
    302         del odict["behaviour"] 
    303         return odict; 
    304      
    305     def __setstate__(self, state): 
    306         self.__dict__.update(state)         
     300    def prepareStateForSaving(self, state): 
     301        """Prepares state for saving 
     302        @type state: dictionary 
     303        @param state: State of the object   
     304        """ 
     305        GameObject.prepareStateForSaving(self, state) 
     306        del state["behaviour"] 
    307307 
    308308    def createBehaviour(self, layer): 
  • trunk/game/scripts/objects/base.py

    r541 r542  
    5959class DynamicObject (object): 
    6060    """A base class that only supports dynamic attributes functionality""" 
    61     def __init__ (self, name="Dynamic object", image=None, **kwargs): 
     61    def __init__ (self, name="Dynamic object", image=None, engine = None, **kwargs): 
    6262        """Initialise minimalistic set of data 
    6363           @type name: String 
     
    6767        self.name = name 
    6868        self.image = image 
     69        self.engine = engine 
     70 
     71    def prepareStateForSaving(self, state): 
     72        """Prepares state for saving 
     73        @type state: dictionary 
     74        @param state: State of the object   
     75        """ 
     76        del state["engine"] 
     77     
     78    def restoreState(self, state): 
     79        """Restores a state from a saved state 
     80        @type state: dictionary 
     81        @param state: Saved state   
     82        """ 
     83        self.__dict__.update(state) 
     84 
     85    def __getstate__(self): 
     86        odict = self.__dict__.copy() 
     87        self.prepareStateForSaving(odict) 
     88        return odict 
     89     
     90    def __setstate__(self, state): 
     91        self.restoreState(state) 
    6992 
    7093    def trueAttr(self, attr): 
  • trunk/game/scripts/objects/containers.py

    r539 r542  
    9292        self.createBehaviour(agent_layer)         
    9393         
    94     def __getstate__(self): 
    95         odict = self.__dict__.copy() 
    96         del odict["behaviour"] 
    97         return odict 
    98      
    99     def __setstate__(self, state): 
    100         self.__dict__.update(state) 
     94    def prepareStateForSaving(self, state): 
     95        """Prepares state for saving 
     96        @type state: dictionary 
     97        @param state: State of the object   
     98        """ 
     99        ImmovableContainer.prepareStateForSaving(self, state) 
     100        del state["behaviour"] 
    101101     
    102102    def createBehaviour(self, layer): 
Note: See TracChangeset for help on using the changeset viewer.