Save and load for my little text game

I want to make a save and load system. So, I have a def materialInv, def inventory and a class player, which has a pokeballs, coins and other…
I want all of this to be saved, but idk how.

Can you please help?

Repl link/Link to where the bug appears

So, yeah, you should watch on the line 46, 61, 78.

Hi @kk284 !
You can create a save and load system using the sqlite3 Python module, or a txt file to save user data.

It seems like something hard… Maybe pickle? It’s easier I thing, but idk how to use it right so.

You can use replit database, if you want to load different saves, for example you can create 3 saves.
eg.

from replit import db
#example
if not db['save1']:
  db['save1'] = {}
  db['save2'] = {}
  db['save3'] = {}


def save_game(save):
  #You can implement a check over here to see which save the user loaded on. For example:
  if save == 1:
    db['save1'] = {'key1':'val1','key2':'val2'}
  #and so on.

#retrieve the data
def load_data(save):
  #again, implement the check
  if save == 1:
    variable1 = db['save1']['val1']
    variable2 = db['save1']['val2']
  #and so on.

#check which save they are on when they run the program
x = input('what save do you want to load on?\n\nsave 1\nsave2\nsave3\n\n ->')
if x == '1': # remember this is a string not an int
  save = 1

#or you can implement this in a function
def load_save():
  x = input('What save do you want to load on?\n\nsave 1\nsave2\nsave3\n\n ->')
  if x == '1': # remember this is a string not an int
    return 1

#now...
x = load_save()
#remember the parameters

#load data
load_data(x)

#save data
save_game(x)

and now goodbye i am gonna go play some Geoguessr

1 Like

Unfortunately, it can’t be used when published.
Why, ghost forks, why!?

1 Like

How the hell you learning this… Also there is a problem. The console saying that there is no module named replit.

If you want, you can use this ReplitDB documentation page which might help you with using ReplitDB. But Like @NateDhaliwal said, its probably not a good idea to use at the moment due to ghost forks.

If you want a local save/load method, you can use JSON (JavaScript Object Notation) to store game data.
Here is a w3schools page about using JSON in python: https://www.w3schools.com/python/python_json.asp

If you need more help, feel free to Ask!

1 Like

Thank you so much @SnakeByte

1 Like

Could you please enlighten me on whether you intend to allow data saving and loading across several devices or individuals, or is your preference inclined towards saving this data locally, solely for one individual’s use?

1 Like

Utilizing Replit DB, you can establish a back-end API, so that you can facilitate the creation of a database over several instances. This progressive approach differentiates from the traditional method of database creation, which is confined to a local setting, reserved solely for one user, and is potentially susceptible to deletion.

Actually, that won’t work either because of ghost forks. A ghost fork is pretty much like a normal fork:

  • Repl DB Data is not copied to the fork, and modifications made in the fork are not copied to the original repl
  • Same for secrets
  • The filesystem is copied to the ghost fork, but just like normal forks, modifications to the file system are not copied back. This means that if the user comes back after a while and runs the repl again (it will probably create a new fork) all of their data will be lost

Any of the solutions listed here will work (I would recommend pickle/json for serialization, then writing the data to the filesystem as I think repl DB has serious egress issues), BUT they need to be done in a separate repl. (When this is done with repl DB, it’s known as a database proxy. I think this is what Sky is recommending)

You could have a separate repl that maintains an HTTP endpoint (HTTP repls do not get ghost forked). The repl that does get ghost forked can then make HTTP requests to this repl to save/load the data.

However, you MUST implement some form of authentication (repl identity is insecure, I would recommend using a repl auth based solution) and authorization (something like you can only edit the key that corresponds to your username) to make this secure.

I’m pretty sure that even saving data locally for one person won’t work with ghost forks. (But I might be wrong)

3 Likes

Yeah, that’s what I’m suggesting.

1 Like

and to add on, you can use JSON using my package together with replit db.


Please do note to do this in shell:

pip install idkwhttph

OR

poetry add idkwhttph

here is an example of how to use it:

from idkwhttph import create_backup,load_backup,save_backup,sync_backup()
from replit import db
#example
if not db['save1']:
  db['save1'] = {}
  db['save2'] = {}
  db['save3'] = {}
  #after all the database keys...
  create_backup(db) #create 'save1' 'save2' and 'save3' Dont forget parameter!

def save_game(save):
  #You can implement a check over here to see which save the user loaded on. For example:
  if save == 1:
    db['save1'] = {'key1':'val1','key2':'val2'}
    #sync data
    sync_backup(db)
  #and so on.

#retrieve the data
def load_data(save):
  #this time, before checking, make sure to sync the data
  sync_backup(db)
  #again, implement the check
  if save == 1:
    variable1 = db['save1']['val1']
    variable2 = db['save1']['val2']
  #and so on.

#check which save they are on when they run the program
x = input('what save do you want to load on?\n\nsave 1\nsave2\nsave3\n\n ->')
if x == '1': # remember this is a string not an int
  save = 1

#or you can implement this in a function
def load_save():
  x = input('What save do you want to load on?\n\nsave 1\nsave2\nsave3\n\n ->')
  if x == '1': # remember this is a string not an int
    return 1

#now...
x = load_save()
#remember the parameters

#load data
load_data(x)

#save data
save_game(x)
sync_backup(db)

also you dont need to create a new file to store the data, the code does it for you.

1 Like

But how does this solve the problem of persisting data across ghost forks?

2 Likes
  • create file using create_backup(db)
  • fork repl
  • you get the code + the file
  • use sync_backup(db)
  • all done

Yes, and when the user comes back after some time and a new ghost fork is created, they can’t access their data anymore

2 Likes

New ghost forks are automatically made???

1 Like

Yes. you are right. But I meant saving the JSON file locally to the user’s device for a local solution.