I write a save_exit function, so whether you're closing the game via menu, alt f4, or otherwise, you only need to say save_exit(); and be done. Kinda surprised no one else said something like this.
This makes it so that you can change what happens on exit if you want to add/remove features, too. You could just have two lines: save and exit. You could have saving game data and settings in separate functions, if, calling them individually in different contexts while the game is running, but calling both on exit to be sure nothing was missed, like a last-second bgm volume adjustment. You could have everything fade out, and if you want to be able to fade music and sfx, both separately and together, you can do that. If you want to add something later, but would rather not do it now, for whatever reason.
Point is, even if all the function does is call two other functions, for this purpose, it's still a good idea to give it a function, unless you know for sure that there is only one way to both save and exit, and that will always be the case in this project.
[edit]Also, separating actions from input is a good habit to get into, if you have aspirations of online multiplayer, allowing joysticks, etc. Trust me, building for one set of keyboard controls, then trying to add controllers or networking or different keyconfigs on top of that, gets real messy real fast. Even if it works, diagnosing the inevitable bugs just gets harder. And, hey, what if you have loops that block the main loop? Is the player stuck, unable to close the game, until they find a way out of that loop? Not if you have a function that checks for all the system-level commands (like alt f4) and handles them. You can just put that one line at the top of every while-loop. Since you might accidentally wind up in an infinite loop, this is as much for your convenience as it is players'. [/edit]
看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
George... Don't do that.