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.
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)
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
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?
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)
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.