Q_A
Q_A

Reputation: 451

C++ - Restarting a game by calling the main() function

I'm building a small game. One of the input options is to restart the game. The only way I could think of doing this was to call the main function from within the main function

int main(int argc, char argv[]) {
 ...
 if (input == "restart") {
  main(argc, argv);
 }

Is this bad form? Will it even work?

Upvotes: 4

Views: 12936

Answers (6)

super
super

Reputation: 12928

If in addition to reloading internal resources, you also need to reload external things like a libraries that the game links to you can do this by re-launching the game in a thread, detaching the thread and then shutting down.

I use this in a game I've made where I have automatic updates, to start the new updated executable and libraries.

int main() {

    //initialize the game

    bool restart=false, quit=false;

    while (!quit) {
        //Main loop of the game
    }

    if (restart) {
        #ifdef _WIN32
            std::thread relaunch([](){ system("start SpeedBlocks.exe"); });
        #elif __APPLE__
            std::thread relaunch([](){
                std::string cmd = "open " + resourcePath() + "../../../SpeedBlocks.app";
                system(cmd.c_str());
            });
        #else
            std::thread relaunch([](){ system("./SpeedBlocks"); });
        #endif
        relaunch.detach();
    }

    return 0;
}

A bit of a hack, but it gets the job done. The #ifdefs just make it use the correct launch cmd for Windows/Max/Linux.

Upvotes: 0

Sherif O.
Sherif O.

Reputation: 514

you can use GOTO but this is not a good way of programming in general. As the guys mentioned to use booleans or loops to check the current state or any other way instead of goto because it causes sometimes problems in the compiler. However it is still available in C and not C++ (AFAIK)

Upvotes: 0

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

You can't call main() recursively. That's actually undefined behavior.

Use a loop instead:

int main() {
     bool restart = false;
     do {
         // Do stuff ...

         // Set restart according some condition inside of the loop
         if(condition == true) {
             restart = true;
         } // (or simplyfied restart = condtion;)
     } while(restart);
}

Upvotes: 9

Unimportant
Unimportant

Reputation: 2096

Since recursively calling main is impossible in C++ and would not really solve the problem, here's my 2 cents on how to deal with the problem:

Basically, any large program is a loop that might look like this:

int main()
{
    bool quit = false;

    //Initialise and aquire resources...
    while (!quit)
    {
        //Run game and set quit if user wants to quit...
    }
    //free resources, should be automatic when RAII is adhered.
}

Your game should already look something like this, since any program that is not a loop will immidiately quit and won't be much of a game. Just change the structure to this:

int main()
{
    bool quit = false;
    bool restart = false;

    while (!quit)
    {   
        Restart = false;
        //Initialise and aquire resources...
        while (!quit && !restart)
        {
            //Run game and update quit and restart according to user input.            
        }
        //free resources, should be automatic when RAII is adhered.
    }
}

Upvotes: 1

kmdreko
kmdreko

Reputation: 59862

Do not do this. From http://en.cppreference.com/w/cpp/language/main_function

The main function has several special properties:

1) It cannot be used anywhere in the program

a) in particular, it cannot be called recursively

Upvotes: 5

Leandros
Leandros

Reputation: 16825

No, the C++ standard disallows calling main manually.

To cite the standard (C++11: 3.6.1 Main Function)

The function main shall not be used within a program. The linkage (3.5) of main is implementation-defined. A program that defines main as deleted or that declares main to be inline, static, or constexpr is ill- formed. The name main is not otherwise reserved.

Upvotes: 12

Related Questions