Writing game-state data...

Prasad, Ramit ramit.prasad at jpmorgan.com
Fri Nov 9 18:23:06 CET 2012


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 use shelve 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 a built-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 needed any 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.  



More information about the Python-list mailing list