Basically, I am making a text-based python game and have come across a block.
The player can enter a battle and as always, there is a chance you would die (hp is 0). What I want to do is to break the main loop of the game, which is several functions down the road, roughly 5-6 (from main to the combat system function), dispersed in different files.
The structure is as follows:
core()
while True:
main()
game_over()
- core - runs main function and game_over() in a While loop
- main function runs the game
- main calls event function
- event function does something depending on conditions/circumstances (or nothing if none are met) -> the function calls the combat loop whenever the player writes 'battle'
- quite a bunch of functions are used in the combat loop, but what the loop does is eventually check if the player's hp is greater than 0.
What I want to do is that whenever the player's hp goes 0 (or even below), the main() function would be broken so that the player can then be prompted to either exit the game or run the main function once more. I would like this to happen differently from having to type return and a value after each function.
The game_over function prompts the player to type in "new" and start main() again as the function would simply not do anything, or "quit" and the function would call quit() and exit the game.
TL:DR - How can I break out of a function all the way from a smaller one? Trying to avoid an array of returns?
returnis the only way.