What’s the best methods for saving and loading a game?

Perhaps being a bit more specific might help more people help you? :smiley:
I assume you mean “what is the best way to save and load the position and state of every dynamic object in a game level as well as the players current progress through a story line”?

Or you could be asking what is the best method to physically store the info in a file, or something else?

You might also be asking about the best way to hide the saving process from the player? Ie, make it look like there are no ‘loading zones’ - like in some of the GTA games?

Could you clarify what you are asking about…? :wink:

No. I don’t think there is a need to specify. Saving a game means exactly that. loading a game is also clear.
But for those who need a more specific query. :wink:

I have a button marked ‘save’. When I click the button, the state of the game, be it players position, other entities position, etc. are saved, (as in most games I know) so that when the player exits the game, and returns another time, when he clicks on the button marked ‘load’, the game will start from the state it was in when he pressed the ‘save’ button. Hope that is clear enough. :slight_smile:

I wanted to know what the best method was, because I saved a file containing the position, rotation, etc of the player, as well as the state of various variables. But when I retrieve the info from the file in order to load it, I get an error about string and interger, I suppose because the position and rotation are read as a string, not an interger. So I was wondering if there is another method that I could use.

Thanks for replying. :slight_smile:

You perty much answer your self on the “best” way to do it. Really, we don’t know what you want to save, so thats up to you, but to turn strings and ints into what you need use str(), int()…

You can also look here

Thank You.
That’s a lot of work for a person who has about 100 active entities.

Also, if your game for networking/online, really you don’t need to save anything on the client side, but the on server side only for security reasons.

Things you may want to save:
Passwords, Name, items they may have, pos, and health or health type bars(aka like mana), chat, and what “quest” they did or on.

Also may want to keep a 10-15 lines of all thos so you can see if someone maybe cheating by checking to see if it “jumps” way to fast for no reason, this also nice for chats when ppl start to rag on other players, you can see what they are saying, also with spamers.

Don’t know what you mean by that ^^: but your welcome…

Also alot of of the questions around here are more on python then panda it self, so just check around the net for alot of the questions you may have.

You may want to take a look at the pyyaml library. In the most simple case:

import yaml
class Game:
  def save(self):
    stream = file('', 'w')
    yaml.dump(self, stream)

This will store the game class instance in a human readable plain text file with the yaml syntax. Ideally you would want a more elegant solution then a raw dump ( at the very least have the save/load function in another class).

You can also just use pickle.


Thanks guys.
I’ll take a closer look at what you posted croxis. It like it would save a lot of work. Hopefully I can get it use.

I got mine working though, and it’s working perfectly.
Here’s the code for all who would like to use this method:

    """Save Game"""
    def saveGame(self):
        if self.saved == 0:
            self.saveFile = open(str(self.playerName)+'.txt', 'w')
            # Save a file in the players name. In this case whenever he saves the game, it will always overwrite the file with his name. (Ofcourse you can make it that the player can save many files under many different names. Whenever he loads the file with his name will load.
            self.saveFile.write("## STAGES\n"+str(self.steps)+"\n## MOD ROTATION\n"+str(self.Mod1.getH())+"\n"+str(self.Mod1.getP())+"\n"+str(self.Mod1.getR())+"\n## MOD POSITION\n"+str(self.Mod1.getX())+"\n"+str(self.Mod1.getY())+"\n"+str(self.Mod1.getZ())+"\n## SCORE\n"+str(self.score)+"\n## WORD\n"+str(self.wordIs)+"\n## POINT POSITION\n"+str(self.waypoint.getX())+"\n"+str(self.waypoint.getY())+"\n"+str(self.waypoint.getZ())+"\n## CAM POSITION\n"+str(self.my_camera3.getX())+"\n"+str(self.my_camera3.getY())+"\n"+str(self.my_camera3.getZ())+"\n## CAM ROTATION\n"+str(self.my_camera3.getH())+"\n"+str(self.my_camera3.getP())+"\n"+str(self.my_camera3.getR())+"\n")
            self.saved = 1

    """Load Game"""
    def LOAD(self):
        # Check if log file exist
            self.checkFile = open(str(self.playerName)+'.txt')
        except IOError: # If an error occurs...
            self.helpIV = Sequence(Parallel(Func(self.narLetter_0.__setitem__, "text", "There is no saved file. Click the ball to start."), Wait(2))
            self.checkFile = open(str(self.playerName)+'.txt', 'r')
            # Use linecache to get a lines 1 - 5
            self.player_save1 = linecache.getline(str(self.playerName)+'.txt', 2).rstrip("\n")
            self.player_save2 = linecache.getline(str(self.playerName)+'.txt', 4).rstrip("\n")
            self.player_save3 = linecache.getline(str(self.playerName)+'.txt', 5).rstrip("\n")
            self.player_save4 = linecache.getline(str(self.playerName)+'.txt', 6).rstrip("\n")
            self.player_save5 = linecache.getline(str(self.playerName)+'.txt', 8).rstrip("\n")
            self.player_save6 = linecache.getline(str(self.playerName)+'.txt', 9).rstrip("\n")
            self.player_save7 = linecache.getline(str(self.playerName)+'.txt', 10).rstrip("\n")
            self.player_save8 = linecache.getline(str(self.playerName)+'.txt', 12).rstrip("\n")
            self.player_save9 = linecache.getline(str(self.playerName)+'.txt', 14).rstrip("\n")
            self.player_save10 = linecache.getline(str(self.playerName)+'.txt', 16).rstrip("\n")
            self.player_save11 = linecache.getline(str(self.playerName)+'.txt', 17).rstrip("\n")
            self.player_save12 = linecache.getline(str(self.playerName)+'.txt', 18).rstrip("\n")
            self.player_save13 = linecache.getline(str(self.playerName)+'.txt', 20).rstrip("\n")
            self.player_save14 = linecache.getline(str(self.playerName)+'.txt', 21).rstrip("\n")
            self.player_save15 = linecache.getline(str(self.playerName)+'.txt', 22).rstrip("\n")
            self.player_save16 = linecache.getline(str(self.playerName)+'.txt', 24).rstrip("\n")
            self.player_save17 = linecache.getline(str(self.playerName)+'.txt', 25).rstrip("\n")
            self.player_save18 = linecache.getline(str(self.playerName)+'.txt', 26).rstrip("\n")

            # The strings must be converted, otherwise the will not work. Convert string (str) to integer (int). Convert string to float
            self.stage = int(self.player_save1)
            self.score = int(self.player_save8)
            self.word = str(self.player_save9)