I am trying to make a save system for my game, and I am encountering an issue where anytime I use my save function, which saves a list onto a empty .py file, it closes the game.
#in Save.py
inv = []
#on the first run of the program this list will be empty, but as the player collects items, those items are added to this list.
def save_func():
file_builder = open("Save.py", "w+") #instantiates the file
file_builder.write("inv = [") #this is my ghetto solution to writing lists into .py files
for x in inv:
file_builder.write("'")
file_builder.write(x)
file_builder.write("', ")
file_builder.write("]\n") #this is the end of ghetto solution to writing lists into .py files
#file_builder.write("player_hp = " + str(player_hp)) #adds variable player_hp into Save.py (FOR FUTURE REFRENCE)
file_builder.close() #closes the file_builder so it doesn't run infinitely
choose_poi()
#main menu/startup "move" command
def choose_poi():
cls.cls()
equipment()
player = input('Type "city" to go to the City\nType "forest" to go to the Forest\nType "inv" to check your inventory\nType "craft" to craft an item\nType "save" to save your progress\nType "stop" to close the program\n')
if(player == "city"):
enter_city()
if(player == "forest"):
enter_forest()
if(player == "inv"):
invcheck()
if(player == "craft"):
crafting()
if(player == "stop"):
sys.exit()
if(player == "save"):
save_func()
choose_poi()
is quite an ugly function, but it has worked so far, only now have I encountered problems with it.
This is the function in question choose_poi()
is my default menu for the game.
I copied this straight from my code, so there are notes to help me remember what everything does.
I tried moving the close()
method to outside of the function so that file_builder
closes whenever the player closes the game.
I also tried having the close()
method immediately after save_func()
like this v
save_func()
file_builder.close()
That didn't fix my issue.
I am thinking it is because of choose_poi()
as I've had a similar issue in the past, but that issue was fixed by adding choose_poi()
to the end of any function I created that was supposed to cycle back to choose_poi()
.
This issue was not fixed by doing the same as the issue I detailed.