0

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.

Connor
  • 13
  • 5
  • You can't close something which is inside the function, outside the function.. – Zero Jul 05 '23 at 14:46
  • 6
    Don't save data as code. It's gonna be a **nightmare** for anything even slightly more complex. – matszwecja Jul 05 '23 at 14:46
  • @Zero That's what I thought, and so I after I tried it and it didn't work, I changed the code back to the big block I have in my question. – Connor Jul 05 '23 at 14:48
  • Your current code is closing the file, not sure about `choose_poi` since its definition isn't provided. – Zero Jul 05 '23 at 14:50
  • @matszwecja This was specifically to save a list variable that I import in. I won't use .py files in the future to save more complex data such as story progress. I'm instead going to save those as either .txt files or some other form of data storage. – Connor Jul 05 '23 at 14:50
  • @Zero Okay so `file_builder.close()` will close both the program and `Save.py`? – Connor Jul 05 '23 at 14:51
  • 3
    As @matszwecja said, don't write code with code, if it's a list variable, you can save it as JSON, and `file_builder.close()` will only close the file object.. – Zero Jul 05 '23 at 14:53
  • 3
    @Connor It's still a terrible idea. [`json`](https://docs.python.org/3/library/json.html#module-json) for example is much better suited for storing things like lists. Also, consider switching to [`with`](https://stackoverflow.com/questions/9282967/how-to-open-a-file-using-the-open-with-statement) statements instead of manually opening and closing files. – matszwecja Jul 05 '23 at 14:53
  • Its difficult to see what is happening. Where is `save_func()` being called? It is suspicious that you are calling `choose_poi()` from within this function. That sounds like unnecessary recursion. – quamrana Jul 05 '23 at 14:54
  • @Zero okay, so disregarding how I'm storing the variable, why is my program crashing whenever I run `save_func()`? Is it because it does not cycle or because of some other issue in the code? – Connor Jul 05 '23 at 14:55
  • 1
    Is there any specific error when it crashes? – matszwecja Jul 05 '23 at 14:56
  • @quamrana `choose_poi()` is used to cycle my program so that the game doesn't immediately close after the player finishes all of one route of actions. I had the issue a lot when I first started making this game, and this was the best solution I found. – Connor Jul 05 '23 at 14:57
  • @matszwecja no, it doesn't return any errors, and when I run it in terminal it works as intended, it is only when I run the .py file itself. – Connor Jul 05 '23 at 14:58
  • Unnecessary recursion cannot be *the* best solution. When you want your program to `"go back to ..."` you generally want a loop to do that, not recursion. – quamrana Jul 05 '23 at 14:59
  • 1
    probably the program is just ending naturally. but it seems like the reason is in the part of code you haven't posted here – Anentropic Jul 05 '23 at 14:59
  • @Connor, running the `.py` directly isn't crashing the program, it runs the same as the terminal the difference being it runs in IDLE by default and closes immediately upon execution. So, its not crashing but completion. – Zero Jul 05 '23 at 15:00
  • @Zero the `inv` list is stored in `Save.py` as well as an empty version in my main .py file. The empty version in my main .py file gets added to as the player collects items, and then they can run `save_func()` to write the contents of that `inv` onto the `inv` in `Save.py`. – Connor Jul 05 '23 at 15:01
  • Is `inv` a global variable? – Zero Jul 05 '23 at 15:01
  • 1
    Not related to the problem, but why do you have all those `global` declarations? The function never uses those variables, let alone assigning them. – Barmar Jul 05 '23 at 15:01
  • @Zero I did not know that, thanks for telling me that. So, how would I best keep it from completing? It seems both you and quamrana have ideas on how to fix that. Where can I post my entire program so that we can better discuss how to optimize the looping? – Connor Jul 05 '23 at 15:04
  • @Zero yes `inv` is a global varible. – Connor Jul 05 '23 at 15:04
  • @Barmar I forgot to remove the `player_hp` and other variables while coding. I am changing that as I respond. – Connor Jul 05 '23 at 15:05
  • @Connor, try to complete your code as much as possible, so we can better understand how you're trying to use these variables and functions. – Zero Jul 05 '23 at 15:06
  • In general terms, to `"keep it from completing"` you just need a `while True:` loop. – quamrana Jul 05 '23 at 15:07
  • 1
    `file_builder.close()` won't close the program, it just closes the file. BTW, you should learn to use `with` so you don't have to explicitly close the file when you're done writing. – Barmar Jul 05 '23 at 15:08
  • @Zero okay, I'll edit my question to contain all the related portions of this function. – Connor Jul 05 '23 at 15:08
  • @Barmar I'll be sure to learn how to do that. – Connor Jul 05 '23 at 15:09
  • I've added the `choose_poi()` function in its entirety. – Connor Jul 05 '23 at 15:14
  • @Connor, please add the definition of `inv` as well, since you're using it in the function that's causing you the issue. – Zero Jul 05 '23 at 15:18
  • Yes, your `save.py / save_func()` is just a red herring here (worthy of addressing in a separate question) and does not contribute to your program stopping. – quamrana Jul 05 '23 at 15:24
  • @Zero *"it runs in IDLE by default"* -- Just to avoid confusing OP, it depends on your setup whether a .py file runs in IDLE or some other way. – wjandrea Jul 05 '23 at 15:30
  • @wjandrea Thanks for the clarification. I'd like to say when I said "terminal" I meant the terminal native to VS Code, when I run the .py file directly, I am not going through VS Code or Command Prompt/Windows Terminal. I am just double clicking the .py file. – Connor Jul 05 '23 at 15:33
  • BTW, welcome to Stack Overflow! Check out the [tour] and [ask], which has tips like how to write a good title. I say that because the title here turned out to have nothing to do with the issue, plus the "write() function" part doesn't really make sense, cause you don't close functions per se. Anyway, making a [mre] can help you narrow down problems in the future; you might want to [use a debugger](/q/4929251/4518341) to help with that. See also [What is the proper way to approach Stack Overflow as someone totally new to programming?](//meta.stackoverflow.com/q/254572/4518341) – wjandrea Jul 05 '23 at 17:17

1 Answers1

0

It looks like you just need a while True: loop within this function:

def choose_poi():
    while True:
        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"): 
            break 
        if(player == "save"):
            save_func()

and just remove any calls back to choose_poi() from your other functions.

Also the "stop" just needs a break now.

Your problems with save_func() are just a red herring. Those are worthy of asking a new question.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • That is a lot more simple than I thought it'd be. Thank you for your help. – Connor Jul 05 '23 at 15:18
  • So I'm coming back to say that this solution did not fix my problem and instead just broke the cycling so that it doesn't happen at all. I reverted the changes and it went back to normal, I have not tested the save system yet, but I am going to assume that I will have the same problem I had before your suggested changes. – Connor Jul 07 '23 at 15:39