With the visuals set, next we will provide the scripted functionality for our Pause Menu.
Click the Graph button in the upper-right corner of the Widget Blueprint Editor window.
On the Graph tab in the My Blueprint window, click the QuitButton then in the Details panel add an OnClicked Event.
Also add an OnClicked Event for the ResumeButton.
In the Graph window, Right-click and add a Get Player Controller node.
Off the Get Player Controller, use Set Input Mode Game Only then Remove from Parent and Set Game Paused as shown below.
Here when the Resume Button is clicked, the input mode is set to game only and the cursor that is displayed when the Pause Menu is activated is removed. The Remove from Parent node is used to remove the actual Pause Menu widget from display before leaving the paused state by using the Set Game Paused node and leaving Paused unchecked.
Create a new Function called Remove HUD.
We will use this to remove the game HUD from the player's view when the Pause Menu is activated.
In the Function, add a Get Player Character node and off the Return Value, add the Cast ToFirstPersonCharacter node.
This will allow us to access the Character Blueprint being used by the player and the game HUD that is being used by that player.
Off the As First Person Character, use Get HUD Reference then Remove from Parent as shown below.
This will access the HUD being used by the player character and remove it from the viewport when quitting the game.
On the Event Graph, off Get Player Controller use the Set Show Mouse Cursor node set to True.
When Quit is clicked, this will turn on the mouse cursor for menu navigation when returning to the Main Menu.
Next add the Remove HUD function created above followed by Open Level (set to Main or the name of the Level that you have your MainMenu on) and connect as shown.
Click for full view.
Both Buttons are now setup, the last thing that we need to do is allow the player to call the Pause Menu and pause the game.
In the Content Browser under Content/FirstPersonBP/Blueprints, open the FirstPersonCharacter Blueprint.
In the Graph, add a M Key Event and a Create Widget node (set to Pause Menu) and promote the Return Value to a variable.
While we used the M Key as our input for bringing up the Pause Menu, you can use any key you wish. For the promoted variable, we called our variable Pause Menu Reference as this will store a reference to created Pause Menu.
Hold Ctrl and drag in the variable from the MyBlueprint window and connect it to an IsValid node.
We are checking to see if the Pause Menu widget has been created and stored in this variable yet.
Connect the Is Not Valid pin to the Create PauseMenu_C Widget.
Right-click and add the Get Player Controller node then Set Input Mode UI Only and connect the nodes as shown below.
When M is pressed, if the Pause Menu has been accessed before it will not need to create it again and will instead access the variable. If this is the first time the Pause Menu has been accessed it will create it then store it as a variable so it can be access later. In either case, the input mode is set to UI only before we display the Pause Menu (which we will do next).
Off the Pause Menu Reference variable, use the Add to Viewport node then Set Game Paused (set Paused to True).
Click for full view.
Our Pause Menu is setup, now it is time to test.
Click the Compile and Save Buttons, then the Play button to play in the editor.
Once in-game, if you press the M Key (or whichever key you assigned), you will see that the game is paused and the Pause Menu appears. From here, you can resume gameplay with the Resume button or quit back to the Main Menu with the Quit Button.
The next section will provide you with additional information on working with Unreal Motion Graphics and Blueprints.