SGT RP Command
SGT RP Command

Reputation: 23

Basic savegames for a text based game

OK, I know how basic this will seem but I'm not really mastered the basics yet. So, I'm trying to write variables as text into a file, then retrieve them to use when I restart the game.

Upvotes: 1

Views: 90

Answers (1)

Thomas Orozco
Thomas Orozco

Reputation: 55197

The shelve module is probably what you're looking for. Note that shelve uses pickle internally, so it will be able to handle any Python object.

Here's how it works. First, you open the shelve:

import shelve

data = shelve.open("savegame")

The, you play around with the data:

data["foo"] = "bar"

Then, when you're done, you sync the shelve to the filesystem:

data.sync()

Then, when you re-open the shelve, data["foo"] will still be set to "bar".

Upvotes: 4

Related Questions