• Advertisement
Sign in to follow this  

Alright, this is a really stupid question...

This topic is 3003 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

In my rpg, when you are fighting a monster, and you die, how can i make it so it closes the WHOLE program right there after saying you die? Can you?

Share this post


Link to post
Share on other sites
Advertisement
Generally you can. The best method of doing so would depend on little things like programming language or what platform you're programming for.

Share this post


Link to post
Share on other sites
You can use the standard library function std::exit(), with a suitable status value (0 conventially signals that the program terminated without any errors).

For more complex programs that need to run shutdown code, the best way is usually to allow all functions to return back to main(), which then returns normally. This allows any shutdown code handled by RAII objects to run.

For example, your game might be like this:

void fight(Player &player, Monster &monster)
{
while(player.alive() && monster.alive())
{
player.attack(monster);
monster.attack(player);
}
}

int main()
{
Player player;

while(player.alive())
{
fight(player, getRandomMonster());
increaseStats(player);
}
}


You can see from the above code that when the player dies, the fight() function will return, and so will the main() function.

Share this post


Link to post
Share on other sites
A program's 'natural' state is to close immediately anyway. Instead of asking how to make your program exit at a particular point, ask yourself: why is it staying open at a particular point? If you understand why it's staying open, you can work out how to manipulate that to make it stop staying open.

Share this post


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

  • Advertisement