So I am making a python game and I am trying to implement a save/load system. I have the save part down however the load function that I made isn’t working. When I assign cPickle.load to a new list it isn’t registering.
def save():
file = open('save.txt', 'wb')
cPickle.dump(GameState, file)
file.close()
def load():
inFile = open('save.txt', 'rb')
newList = cPickle.load(inFile)
inFile.close()
Please help, thanks!
You probably forgot to return your list in
load:Note that a more Pythonic to load your file is:
Here, we use what is called a context in Python (the
with...asstatement), quite useful to make sure your file is automatically called. It’s also a good idea not to hard-code the name of your file in the function, but pass it as an argument.When you call your
loadfunction, you will get what you put in your pickle, aGameStatein your case.