[Tutor] How to replace instances

Kent Johnson kent37 at tds.net
Mon Sep 29 12:35:39 CEST 2008


Replying to the list..

On Mon, Sep 29, 2008 at 12:43 AM, Steve Collins <emperor.ghaz at gmail.com> wrote:
> Sorry it took me so long to respond; I haven't been able to afford any time
> to such leisurely pursuits as programming.
> I'm not cc'ing this to the Mailing list, becuase I fear it may get a little
> long.

It's not too long.

>
>> I think you still misunderstand what objects and lists and references
>> are,
>
>
> I agree. :D
>
>>
>>   this misunderstanding is making it hard to for me to
>> understand what you are really trying to accomplish. Perhaps you can
>> give a slightly longer example with real classes and lists, perhaps
>> one that doesn't do what you want, and we can correct it.
>
>
> Alright, this is a pared-down example of my problem. The program I'm trying
> to write is a text-adventure game (seemed like a fun way to learn my way
> around programming)
>
> there are, for example, classes which describe rooms and items:
>
> class Item:
>     def __init__(self, name):
>         self.name = name
>         itemIndex[ name ] = self
>         masterIndex.append(self)
>
> class Room:
>     def __init__(self, name, items):
>         self.name = name
>         self.items = items
>         masterIndex.append(self)
>
> ## which are initialized as they should appear at the beginning of the game
>
> iBook = Item( "book" )
> rLibrary = Room( "library", [iBook] )
>
> ## when the player enters a room, currentRoom is set to the Room being
> entered into. when the player enters the "take" command and specifies
> "book," and sends itemIndex[ "book" ] (which is iBook) to a function is
> called which goes something like:
>
> takeMethod(takeWhat):
>     if takeWhat in currentArea.items:
>          (remove the item from currentArea.items, add it to the player's
> inventory and display "you took whatever")
>     else:
>          (display "you cant take that.")
>
> this works very well, and I seem to have created a working text adventure
> game. my new problem is the save/load function
> if the player saves, masterIndex is pickled to save.dat
> when the game starts again the player has the option of loading from
> save.dat.
> I did this so I could avoid having a file for every pickled instance, but I
> can't figure out what to do with the unpickled list.

You should be pickling all the game objects and restore them from the
pickle *instead* of the normal init. The unpickled masterindex should
become the actual masterindex. This contains all the current items and
rooms. Also pickle the player, to save inventory and location and any
other player attributes. Restore the pickled player to the actual
player.

One way to do this would be to
>
> The closest I came to a solution was this:
>
> L = 0
>
> class Item:
>     def __init__(self, name):
>         if new game:
>             self.name = name
>         elif load game:
>             self.name = loadedList[L].name
>         itemIndex[ self.name ] = self
>         masterIndex.append(self)
>
> class Room:
>     def __init__(self, name, items):
>         if new game
>             self.name = name
>             self.items = items
>         elif load game:
>             self.name = loadedList[L].name
>             self.items = loadedList[L].name
>         masterIndex.append(self)
>
> When the game is loaded, the player can't take the book, becuase the
> takeItem function does not find itemIndex["book"] in the current room,
> because itemIndex ["book"] refers to the newly initialized iBook, instead of
> the old iBook.
>
> Even as I was writing the previous sentence, I realized that if I moved
> itemIndex[ self.name ] = self to the "elif load game" condition and changed
> it to "itemIndex[ self.name ] = loadedList[L]" that would fix this
> particular problem. I tried it with the various dictionaries in the game,
> and it does. But in the actual game, the classes refer to each in much more
> complicated ways, which I fear could break this fix. It also feels clumsy,
> to have all these instnaces which have the exact same data as the loaded
> ones, but don't actually refer to them.
>
> another solution I thought of was this:
>
> if new game:
>     iBook = Item( "book" )
>     rLibrary = Room( "library", [iBook] )
> elif load game:
>     L = 0
>     iBook = loadedLis[L]
>     L +=1
>     rLibrary = loadedList[L]
>     etc., etc.

if new game:
  # normal object initialization
elif load game
  # load masterlist from pickle file
>
> I think this would work well, but, again, it seems clumsy and would also
> triple the number of lines I'd need in the instance initializing portion of
> the program. I may be wrong, but I feel like I should be able to do this
> from a loop. If you have any insight into my problem, I'd greatly appreciate
> it.
>
> Steve

Kent


More information about the Tutor mailing list