Triggering the Pause menu

Now that we have created our Pause menu, we need to create a way for the player to bring up the menu. Traditionally, computer games use the Esc key to pause the game and return to a menu, so we will follow that trope here. First, we will bind the Esc key to a pause Action. Just as we did in Chapter 6, Enhancing Player Abilities, we will add a new Action mapping to Project Settings. On the Edit button in the Unreal Editor menu, select the Project Settings option. On the left side of the window that appears, look for the Engine category and select the Input option. Click on the + sign next to Action Mappings, and then name the mapping as Pause. Use the drop-down menu to select the Escape key mapping, as seen in this screenshot:

Like all other Actions the player can take, we want to establish the functionality of this Action within the FirstPersonCharacter Blueprint. Go to the Content Browser and open the FirstPersonCharacter Blueprint inside the Blueprints folder. In the following screenshot, you can see the nodes we will build in order to create and show the Pause menu:

First, find some empty grid space and add an InputAction Pause Event node. Since bringing up the Pause menu will disrupt the gameplay, it will feel better if the Action is taken only when the user releases the Esc key, as opposed to when they first press it. Drag a wire from the Released output pin of the Escape node, and then attach it to a SET Game Paused node. Ensure that the Paused input checkbox of this node is checked.

With the game paused, we need to enable the mouse arrow so that the player can click on the menu buttons. Start by creating a Get Player Controller node, and then drag a wire from its Return Value output pin to a SET Show Mouse Cursor node. Check the Show Mouse Cursor input box to set the mouse arrow to appear on the screen. Then, connect the input execution pin of this node to the output execution pin of the SET Game Paused node.

With the game paused and the cursor enabled, we can bring up the Pause Menu UI we created. Drag a wire from the output execution pin of the SET Show Mouse Cursor node and attach it to a Create Widget node. Select Pause Menu from the Class input drop-down menu. Then, drag a wire from the Return Value output pin of this node and attach it to an Add to Viewport node. Connect the execution pins between these two nodes in order to finish this series of nodes. Remember to add a helpful comment box around the nodes, and then compile and save this Blueprint.

We will have to make a slight alteration to our testing strategy in order to test the pause menu. The Esc key, by default, closes any active windows that are currently playing the game within the Editor. Thus, the game would close before we could see the pause menu we created. There are two ways by which we can get around this. We can change the key to bring up the pause menu to something other than Esc, such as the P key. Alternatively, we can change the Play mode in the Editor to generate a standalone game window. To follow the latter option, click on the downward-facing arrow next to the Play button, and then select the Standalone Game option, as seen in the following screenshot:

Now, while playing, you should be able to press the Esc key you set up to bring up the pause menu. Clicking on the Resume button should close the pause menu and return you to the game. If you progress several rounds through the game and then press the Reset All button from the pause menu, then you should automatically reload the level, with your progress reset to the first round of the game. If this is what you see, then congratulations! You have accomplished a significant achievement in creating SaveSystem that is able to store, load, and reset progress across multiple rounds of gameplay. If the save system does not work, then check whether BlueprintGameSave is spelled correctly in all Slot Name input fields.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset