Jump to content
  • Advertisement
Sign in to follow this  
RobinOfSweden

Making the whole program end?

This topic is 4325 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I'm creating a Texture-Adventure... the application is built of many functions (each questions is a function(beacause of training purpose)), and if you answer wrong in some cases I want the whole application to end (after saying GAME OVER), im using if conditionals to test the users choice. Is this possible? Thanks/

Share this post


Link to post
Share on other sites
Advertisement
Personally, I find game management to be much easier with a state machine / state stack, but I suppose that doesn't really answer your question.

The easiest way to do it would be to create a boolean variable named Quitting or shutDown or something like that, and when the user fails a question, set it to true.

Also, it may help to know what language you are programming in and what IDE you use.

Share this post


Link to post
Share on other sites
Oh forgot...sorry ^^

Im using C++, and Visual Studio as my compiler, also im working on a Win32 Console Application.

I want it to be like this

if(answer == 1) {
do this;
} else if(answer == 2) {
do this;
} else if(answer == 3) {
do this;
game over, quit game somehow;
}

The conditionals is in separeate functions (each question is a separate function), so I need the main function to end, not just the function itself.

Thanks/ Robin

Share this post


Link to post
Share on other sites
A process ends naturally when it reaches the end of its main/WinMain. The correct way to end execution is to allow all the loops to fall through so main returns (to kernel32, if you were wondering). In your case, this would be best done by setting a 'isRunning' flag (which is true by default) to false. If every part of the game cycle respects this flag, and progresses the game only if (isRunning == true), then upon setting the flag, all your program's guts will fall out and it will end naturally (though last time I checked, disembowelment didn't count as 'natural causes').

If you don't have any resources to free up (or even if you do, but want to be naughty), you can forcibly terminate execution with Kernel32!ExitProcess (or ExitThread on the main thread). This will kill it there and then. The next instruction won't even get a chance to execute. You shouldn't get into the habit of using ExitProcess.

Regards
Admiral

Share this post


Link to post
Share on other sites
Yeah, if you want to do it correctly, you should devise an abortion path that will dump back into the final lines of main(), so it can terminate naturally.

If you dont care, then just do exit(1);

Share this post


Link to post
Share on other sites
In my experience, using exit(1); is perfectly acceptable so long as you remember to deinitialize everything first. Make sure you disable the audio stream, etc. Otherwise it may just keep on playing after the application ends -- or it might cause a crash.

Share this post


Link to post
Share on other sites
Note that the 1 is the program's return value and that a non-zero return value typically indicates an error. exit(EXIT_SUCCESS) or exit(0) would be preferable.

Share this post


Link to post
Share on other sites
Another fun thing to be aware of with exit() is that it only calls the destructors on static objects, for example:

#include <cstdlib>
#include <iostream>

class foo
{
public:
~foo() { std::cout << "foo's destructor called" << std::endl; }
};

class bar
{
public:
~bar() { std::cout << "bar's destructor called" << std::endl; }
};


int main()
{
foo a;
static bar b;

exit(EXIT_SUCCESS);
}



Outputs:
bar's destructor called

Share this post


Link to post
Share on other sites
hehe, yeah. I neglected to mention that, because typically if I'm just throwing an exit() in the program, I (1) it to remind myself that Im actually doing something incorrectly, and might as well tell the OS as well. ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!