RE: Writing game-state data...

Discussion in 'Python' started by Prasad, Ramit, Nov 9, 2012.

  1. Graham Fielding wrote:

    >
    > Hey, folks, me again!
    >
    > I've been puzzling over this for a while now:
    >
    > I'm trying to write data to a file to save the state of my game using the following function:
    >
    > def save_game():
    >     #open a new empty shelve (possibly overwriting an old one) to write the game data
    >     file_object = open('savegame.sav', 'wb')
    >     file['map'] = map
    >     file['objects'] = objects
    >     file['player_index'] = objects.index(player)  #index of player in objects list
    >     file['inventory'] = inventory
    >     file['game_msgs'] = game_msgs
    >     file['game_state'] = game_state
    >     file['stairs_index'] = objects.index(stairs)
    >     file['dungeon_level'] = dungeon_level
    >     file.close()
    >
    > However, while 'savegame.sav' is created in the directory I specify, the function dies on file['map'] = map.
    > This is the end of the stack trace:
    >
    >
    >   File "C:\Python Project\Roguelike.py", line 966, in save_game
    >     file['map'] = map
    > TypeError: 'type' object does not support item assignment
    >


    `file` is the built-in for file objects. I would say you need
    to use file_object[] instead, but it is a file object
    and is not meant for this usage. You can write directly
    to a file but it is easier to use sqllite or shelve/pickle
    libraries. I will useshelve in my example since your code
    is already doing something similar. Do not forget to
    import shelve in your own code.

    def save():
    shelf = shelve.open('savegame.sav', protocol=2)
    # Change pickle protocol if you use Python < 2.3
    shelf['map'] = map
    shelf['objects'] = objects
    shelf['player_index'] = objects.index(player)
    shelf['inventory'] = inventory
    shelf['game_msgs'] = game_msgs
    shelf['game_state'] = game_state
    shelf['stairs_index'] = objects.index(stairs)
    shelf['dungeon_level'] = dungeon_level
    shelf.close()



    > Now, the map is randomly generated -- could that be an issue?
    >


    Both "file" and "map" are built-in keywords and using those
    names for you own variables is called shadowing abuilt-in.
    Shadowing a built-in can be interesting and useful but should
    be avoided. Also, it seems like save() is not in a class nor
    having anything passed in; are all the game states variables
    stored at the module level or something?


    > Should I just scrap the current system and use pickle?


    You are almost there, so I would not bother. Normally I would
    use pickle over shelve; I have not neededany of the advantages
    of shelve and why use a library on top of pickle when I only
    need pickle? Of course, YMMV.


    ~Ramit


    This email is confidential and subject to important disclaimers and
    conditions including on offers for the purchase or sale of
    securities, accuracy and completeness of information, viruses,
    confidentiality, legal privilege, and legal entity disclaimers,
    available at http://www.jpmorgan.com/pages/disclosures/email.
     
    Prasad, Ramit, Nov 9, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BlackHawke
    Replies:
    12
    Views:
    1,593
    Andrew Thompson
    Jan 26, 2004
  2. judith
    Replies:
    0
    Views:
    1,697
    judith
    Nov 1, 2006
  3. Max Kubierschky
    Replies:
    10
    Views:
    1,876
    pabloreda
    Mar 31, 2007
  4. Mark Lawrence

    Re: Writing game-state data...

    Mark Lawrence, Nov 9, 2012, in forum: Python
    Replies:
    0
    Views:
    254
    Mark Lawrence
    Nov 9, 2012
  5. Ian Kelly

    Re: Writing game-state data...

    Ian Kelly, Nov 9, 2012, in forum: Python
    Replies:
    0
    Views:
    261
    Ian Kelly
    Nov 9, 2012
Loading...

Share This Page