Ask coding questions

← Back to all posts
Saving Games in Python
UmarQureshi4 (1)

Hiya!

So, I need help with a function in my game, Stormfront.

Basically, I need a simple way of saving my game using a specific keyword. Is there any answer to my question?

< Umar Qureshi >

Answered by InvisibleOne (3224) [earned 5 cycles]
View Answer
Comments
hotnewtop
InvisibleOne (3224)

The easiest way is just to use replit db.
You can save a key like this:

from replit import db

db[keyname]  = data

and then get the value of a key

from replit import db

value = db[keyname]

An example would be saving a dictionary holding the user data

from replit import db
import os

userData = {
  "name" : "username",
  "score" : 0,
  "level" : 1,
}

# Save it using their replit username

replitUsername = os.environ['REPL_OWNER']

def save(data):
  db[replitUsername] = data

def restore(username):
  return db[username]

# using those functions: 

save(userData)

userData = restore(replitUsername)
UmarQureshi4 (1)

@InvisibleOne

What is the 'import os' for?

ruiwenge2 (1183)

@UmarQureshi4 import os is to get the user who is running the repl

InvisibleOne (3224)

You need os to read env variables, which you use to get the person's replit username with os.environ['REPL_OWNER'] @UmarQureshi4

UmarQureshi4 (1)

@InvisibleOne

What about the colons used?

InvisibleOne (3224)

For the python dictionary? Those just give the key that value. @UmarQureshi4

UmarQureshi4 (1)

If you don't mind, can you demonstrate this in my case - so when the player starts, they are asked to type in their username, and then throughout the game, the player can save their progress. How would that work? @InvisibleOne

UmarQureshi4 (1)

What does each part of the database section do? Can you label it for me, if you don't mind?

@InvisibleOne

UmarQureshi4 (1)

Thanks, @InvisibleOne for the help. You were great in helping me solve this problem, and may our paths cross again.

< Umar Qureshi >

@InvisibleOne

UmarQureshi4 (1)

@InvisibleOne, please keep the 'Save Progress' file - that'll help me throughout the programming of the game.

InvisibleOne (3224)

Sure, that's no problem, let me know if you need any other help @UmarQureshi4

[deleted]

You could also use a txt file and use json to retrieve the files.
Ex:
f = open("hello.txt", "w") # "w" will always overwrite the file
f.write("Hello Text File!")
f.close()
And then:
f = open("grocery.txt", "r")
text = f.read() # this gives us everything back at once
f.close()
print(text)
Then use json to get the data back.
To append use "a" in open("hello.txt", a)

ruiwenge2 (1183)

you mean like saving your scores?

UmarQureshi4 (1)

@ruiwenge2

No, saving your progress in the game - it's a simulation game.

ruiwenge2 (1183)

@UmarQureshi4 maybe you could use the replit database and ask the user their username, and when they're done, save their progess by assigning the key, username, to a value, their progress. you can see how to use the replit database at https://docs.replit.com/tutorials/11-using-the-replit-database