Options for Quit/Close States - Text101

I was surprised that Section 3 didn’t have any way for the players to close or quit out of your game, even when Rick put the instructions in the start state on how to do so.

I found a thread about this already posted (and closed now) below:
When do we add the Quit functionality?

rob’s explanation of a possible reason for this omission made sense. The gist of his comment is that there’s no clean way to “exit” a WebGL game, which is probably what the majority of students built their game for, and that even if it was for a different platform, there are different ways you’d have to handle that for certain platforms, thus making it hard to code for beginners.

His post was really good, even going so far as giving some code examples of how you’d tell the program to stop running on various platforms. But it didn’t give a way to integrate it into your code.

I just wanted to post some code that I think would work if you did want to build a Windows application that could be closed out, and also how I worked around hosting it on WebGL.

If you built a Windows Application, the syntax to close it would be:

Application.Quit();

I was messing around after setting up the states and before finishing Section 3 (and realizing there wasn’t a quit explanation), and decided to try to build a way to quit, as I thought I may have missed it prior, as well as I wanted to use keypad numbers to make choices, not just the number keys.

My code looked like this:

private void ManageState()
    {
        var nextState = state.GetNextStates();

        if (Input.GetKeyDown(KeyCode.Alpha1) || (Input.GetKeyDown(KeyCode.Keypad1)))
        {
            state = nextState[0];
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2) || (Input.GetKeyDown(KeyCode.Keypad2)))
        {
            state = nextState[1];
        }
        else if (Input.GetKeyDown(KeyCode.Alpha3) || (Input.GetKeyDown(KeyCode.Keypad3)))
        {
            state = nextState[2];
        }
        else if (Input.GetKeyDown(KeyCode.Q) || (Input.GetKeyDown(KeyCode.Escape)))
        {
            //For closing out Windows Application
            //Application.Quit();

            //For ending runtime in Unity Editor
            UnityEditor.EditorApplication.isPlaying = false;
        }

        textComponent.text = state.GetStateStory();
    }

As you can see, I just tacked on to our if statements with “||”, which is the symbol for OR, and then captured for the corresponding key on the numpad. I also did the same for ‘q’ and Escape for closing the application. At the time I couldn’t get it to work as I was using Application.Quit() (not knowing that I needed to end the runtime in Unity). I just implemented the above and tried it, and it works great. Problem is there’s no catch to handle the exception bug talked about in Lecture 32.

Easy enough, we’ll just integrate the new code into the for loop so we don’t blow out our array bounds. It’ll look like this:

private void ManageState()
    {
        var nextStates = state.GetNextStates();

        for (int i = 0; i < nextStates.Length; i++)
        {
            if (Input.GetKeyDown(KeyCode.Alpha1 + i) || (Input.GetKeyDown(KeyCode.Keypad1 + i)))
            {
                state = nextStates[i];

                textComponent.text = state.GetStateStory();
            }
            else if (Input.GetKey(KeyCode.Q) || Input.GetKey(KeyCode.Escape))
            {
                //For closing out Windows Application
                //Application.Quit();

                //For ending runtime in Unity Editor
                UnityEditor.EditorApplication.isPlaying = false;
            }
        }
    }

This also works, great, and has the added benefit of not exceeding our array bounds. The final option is for if you do want to share your game through WebGL. For that there’s no real coding solution outside of setting up the option to return to the “Start/Title” screen when the user chooses to quit at Game Over/Game Won states. If the user chooses play again, you can set it to be the Beginning Story State instead. From a design perspective, I think this works in differentiating the choice to the user, even though we can’t actually “close out” the game/browser.

Here’s what I did (since I wanted to show my friends the game through WebGL):

First, I removed the instructions from the Start Screen about quitting out the game:
This:

Became This:

Then you comment out the else if statement dealing with quitting, like this:

private void ManageState()
    {
        var nextStates = state.GetNextStates();

        for (int i = 0; i < nextStates.Length; i++)
        {
            if (Input.GetKeyDown(KeyCode.Alpha1 + i) || (Input.GetKeyDown(KeyCode.Keypad1 + i)))
            {
                state = nextStates[i];

                textComponent.text = state.GetStateStory();
            }
            //else if (Input.GetKey(KeyCode.Q) || Input.GetKey(KeyCode.Escape))
            //{
                //For closing out Windows Application
                //Application.Quit();

                //For ending runtime in Unity Editor
                //UnityEditor.EditorApplication.isPlaying = false;
            //}
        }
    }

(ProTip: When in Visual Studio, you can highlight mutltiple lines of code you’d like to comment out and press Ctrl+K, then C to comment them instantly. You can uncomment the same way byt Ctrl+K, then U)

Finally, go through all your Game Over/Won states, increase the size to 2, and in the new Element 1, link it to your Start Screen, while Element 0 is the screen AFTER pressing 1 on the Start Screen, like so:

The Inspector

When a user encounters a game over (or they survive in my game) they’ll get to choose, and the flow will be like this:

Game Over Screen with Choice

Choosing to continue by pressing 1 will bring them back to the beginning of the story (called Beginning in my game)

Choosing to quit by pressing 2 will bring them back to the instructions/title screen (called Introduction in my game)

I think this makes sense to most people who’ve played games, such as arcade games, where you have a choice to continue by adding in quarters, or quit, where it goes back to the title screen.

So there are some thoughts/experiments you can try if you would like to add in the option of quitting to your game. Let me know what you all think!

Privacy & Terms