Question

I'm teaching myself how to write a basic game in python (text based - not using pygame). (Note: I haven't actually gotten to the "game" part per-se, because I wanted to make sure I have the basic core structure figured out first.)

I'm at the point where I'm trying to figure out how I might implement a save/load scenario so a game session could persist beyond a signle running of the program. I did a bit of searching and everything seems to point to pickling or shelving as the best solutions.

My test scenario is for saving and loading a single instance of a class. Specifically, I have a class called Characters(), and (for testing's sake) a sigle instance of that class assigned to a variable called pc. Instances of the Character class have an attribute called name which is originally set to "DEFAULT", but will be updated based on user input at the initial setup of a new game. For ex:

class Characters(object):
    def __init__(self):
        self.name = "DEFAULT"

pc = Characters()
pc.name = "Bob"

I also have (or will have) a large number of functions that refer to various instances using the variables they are asigned to. For example, a made up one as a simplified example might be:

def print_name(character):
    print character.name

def run():
    print_name(pc)

run()

I plan to have a save function that will pack up the pc instance (among other info) with their current info (ex: with the updated name). I also will have a load function that would allow a user to play a saved game instead of starting a new one. From what I read, the load could work something like this:

*assuming info was saved to a file called "save1"
*assuming the pc instance was shelved with "pc" as the key

import shelve

mysave = shelve.open("save1")
pc = mysave["pc"]

My question is, is there a way for the shelve load to "remember" the variable name assotiated with the instance, and automatically do that << pc = mysave["pc"] >> step? Or a way for me to store that variable name as a string (ex as the key) and somehow use that string to create the variable with the correct name (pc)?

I will need to "save" a LOT of instances, and can automate that process with a loop, but I don't know how to automate the unloading to specific variable names. Do I really have to re-asign each one individually and explicitly? I need to asign the instances back to the apropriate variable names bc I have a bunch of core functions that refer to specific instances using variable names (like the example I gave above).

Ideas? Is this possible, or is there an entirely different solution that I'm not seeing?

Thanks!

~ribs

Was it helpful?

Solution

Sure, it's possible to do something like that. Since a shelf itself is like a dictionary, just save all the character instances in a real dictionary instance inside it using their variable's name as the key. For example:

class Character(object):
    def __init__(self, name="DEFAULT"):
        self.name = name

pc = Character("Bob")

def print_name(character):
    print character.name

def run():
    print_name(pc)

run()

import shelve
mysave = shelve.open("save1")
# save all Character instances without the default name
mysave["all characters"] = {varname:value for varname,value in
                                globals().iteritems() if
                                    isinstance(value, Character) and
                                    value.name != "DEFAULT"}
mysave.close()

del pc

mysave = shelve.open("save1")
globals().update(mysave["all characters"])
mysave.close()

run()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top