Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Writing game-state data...

Reply
Thread Tools

RE: Writing game-state data...

 
 
Prasad, Ramit
Guest
Posts: n/a
 
      11-09-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 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing a program to create a wireless connection. =?Utf-8?B?bWJvd3llckBwdXJkdWUuZWR1?= Wireless Networking 1 10-05-2005 12:59 PM
writing email problems? Bri Firefox 1 05-12-2005 03:03 AM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Unhandled exception in FileStream when writing to a full disk - bug in framework? Amit ASP .Net 8 08-04-2003 03:34 PM
A failure occurred writing to the resources file. Access is denied. -- RESX file is locked? -- WHY? Mark Kamoski ASP .Net 1 07-04-2003 12:02 PM



Advertisments