Sign in to follow this  
DaemonMagus

Program Flow

Recommended Posts

I've got this program that I'm making and I have my main function and my game loop function. At one point, it asks if you want to play again. How would I get it to go back to the beginning of the game loop. The way it is now, it calls it directly from that point, but the problem is as soon as that game loop ends, the program continues from that point, which leads it to the end of the program. Here's an example of what I mean:
int main()
{
    gameloop();
    return 0;
}

gameloop()
{
   if (playagain == true)
       gameloop();  // at end of this function call to game loop
                    // it resumes from here, and ends up 
                    // ending the program  
    else
       return;      // can I use return in this manner to just
                    // exit the function without returning
                    // a value? 
}

Share this post


Link to post
Share on other sites
Make your game loop function return a bool (assuming C++) value. Then, if you wish to play again, return true. Otherwise, return false. Use this return value in your main() function to create another loop that keeps going until gameloop() returns false.

int main()
{
while (gameloop() == true); //<-- notice semicolon; this is
// an empty loop. It just keeps
// checking the condition.
return 0;
}

bool gameloop()
{
//Stuff
//Find out what playagain should equal from the player

return playagain;
}

As a side note, an empty return statement is valid for a function that returns void. It would simply jump out of the function immediately.

Share this post


Link to post
Share on other sites
personally i dont see why you would put your game loop in a function. why not just put it directly in main?


while(playagain)
{
do stuff
}


seeems a lot nicer to me then making a function just to do it... i feel that functions should have a distinct purpose, and shouldnt just be used to make the code look nicer, but thats just MO. unless of course your function is really big, in which case you wanted to split it up into sections, but your game loop shouldnt be that big, and even then i would consider maybe your approaching the problem wrong if the function is huge....

Share this post


Link to post
Share on other sites
You simply won't be able to create a loop using recursion in a C or C++ program. You will have to settle to a simple while( ), for( ) or do while( ) construct, and avoid calling the gameloop( ) function from inside itself.

What Agony proposed is the simplest and cleanest way of handling this.

graveyard filla: a game loop does have a different meaning and purpose (that of repeating a given set of actions until the game ends) than the main function (that of being the program entry point), therefore it makes sense (and is generally advised) to keep your game loop in a function other than main( ).

If you had to add another layer of indirection (the main function calling another function, which then launches your game), not using a function for the game loop would have you copying a large portion of code around, as opposed to a single line.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this