2019-02-16 00:17:56

Hi! So I was wondering if there is a way to make an if statment, in bgt, for example:

if(exit())
{
savedata();
}

or something like that?

----------
“Yes, sir. I am attempting to fill a silent moment with non-relevant conversation.”
“You don’t tell me how to behave; you’re not my mother!”
“Could you please continue the petty bickering? I find it most intriguing.” – Data (Star Trek: The Next Generation)

2019-02-16 00:47:10

Ir, wouldn’t it be easier to just call the savedata function before the exit function? This makes no sence to me.

2019-02-16 01:48:26

You could write a functionexit_game which does this, or do you want to ensure your game always stores it's savedata before exiting, even though it's reached the end of the main function / it returns? If so, you can't. Try to improve the controll flow (if it's called that). For example: do not call a main menu function from within your game, then functions will call eachother and create a good mess. Try to make it so that for example the main function calls a main menu function. This main menu function just keeps running the main menu in a while loop with guard choice == EXIT (EXIT is a constant). If another choice is made, for example start game, execute the relevant function. Notice that from within start game you only have to use the return statement to get back to your menu. And from your menu, hitting exit will get you back to the main function, from where you can safely exit the program. Of course, you can also exit from within the game using the exit() function, so save any data before using exit (your if-statement will simply exit the program, since in an if-statement first it's checked if the condition holds, and for that it has to call exit(), which exits the program and therefor will stop the if-statement because it was part of that program.). Did this answer your question?

Roel
golfing in the kitchen

2019-02-16 02:55:22 (edited by pelantas 2019-02-16 02:56:36)

HI

@redfox

The way on how this is done in our audiocookieclicker game, is that the statements which take care of the saving of the several variables in the game are written down into a seperate script, both the save and load scripts. In the main file, in which i wrote the game, right before the exit(); statement gets executed i called the script for saving, like:

if(menu_result == 7)
{
speech.stop();
save_game();
exit();
}

When you want to load the game of a player automaticly, you have to call the load_game(); script before executing the rest of the code. It is kindof detailed to explain on how this can be done easely, probably someone can add to it for me, cause it is very late here in nl, and i am almost falling asleep while typing this.

But hopefully you get the picture smile

Greetz mike

Visit the following website to see what games we have:
http://www.nonvisiongames.com
Or the following English marketplace to see what retrogames and game merchandise I am selling:
https://www.retro-kingdom.com

2019-02-16 10:55:08 (edited by CAE_Jones 2019-02-16 11:02:24)

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.