Key binding

We are now going to bind a key to open up the pause menu, and only allow this to happen when the player is not in battle (in other words, the player is out in the field). Since we already have a FieldPlayer set up from the previous chapter, we can easily create actions within our FieldPlayer Blueprint class that will control our pause menu. To start, navigate to Blueprints | Open Blueprint Class… | FieldPlayer:

Key binding

At this point, we are going to want to have the pause screen pop up when the player presses a key; in this case, we will use P for pause. To do this, we will first need to create a key event that will fire off a set of actions of our choice after a specific key is pressed. To start this key event, right-click on your Event Graph, which will open up all actions that can be associated with this Blueprint, and then navigate to Input | KeyEvents | P:

Key binding

Once done, this will create a key event for the press and release of P. You will notice that this event has pressed and released executables that work as they are described, an action can occur when the player presses P or when the player releases P. For our actions regarding the pausing of the game and the pop up of the pause menu, we will use the released executable, because in order for the released executable to be called, it would mean that the player has gone through the act of pressing and releasing the key. It is often the best practice for a player to commit to a button press just like a player commits to a move in Chess by letting go of a piece. Before we pop up the pause menu, let us pause the game by creating a call to the Set Game Paused function that can be found by right-clicking in the Event Graph and navigating to Game | Set Game Paused:

Key binding

Within the Set Game Paused node, check Paused so that it is set to true, and link the Released executable of the key event P to the in pin of the Set Game Paused. Your game should now pause whenever the player presses and releases P:

Key binding

From here, we will pop up the pause menu. To do so, upon the game being paused, we will create the main pause screen by right-clicking on the Event Graph and navigating to User Interface | Create Widget. This allows us to create instances of any widget that we made. We can create our Pause_Main here by pressing the Select Class drop-down menu within Create Widget and selecting Pause_Main:

Key binding

Next, we can link the out pin of Set Game Paused to the in pin of the Create Pause_MainWidget:

Key binding

This will make it such that after the game is paused, Pause_Main will be created. Although we are creating Pause_Main, this will still not pop up on screen until we tell it to draw on screen. To do so, we will need to create a call to the Add to Viewport function that can add any graphic to a viewport. To do this, left-click and drag out the Return Value pin from the Create Pause_Main widget node, and select User Interface | Viewport | Add to Viewport. This will create a new Add to Viewport node:

Key binding

If you test this, you will notice that the game pauses and the pause menu pops up, but it is missing a mouse cursor. To add the mouse cursor, we will first need to get the player controller by right-clicking on the Event Graph and navigating to Game | Get Player Controller | Get Player Controller:

Key binding

From here, simply left-click and drag out the Return Value pin from the Get Player Controller node, then select Variables | Mouse Interface | Set Show Mouse Cursor:

Key binding

Once done, link the out pin of the Add to Viewport node to the in pin of Set Show Mouse Cursor. What this will do is set the Show Mouse Cursor variable (which is a Boolean) to either true or false after the pause menu has been displayed in the viewport. The Set Show Mouse Cursor variable needs to also get the player controller because the player controller holds the mouse input information.

If you playtest this now, you will notice that the mouse cursor still does not show up; this is because Show Mouse Cursor within Set Show Mouse Cursor is unchecked, meaning that Show Mouse Cursor is set to false, so check the box whenever you want to show the mouse cursor.

At this point, your menu should pop up perfectly after pressing P and the mouse should be completely visible and controllable. Your level blueprint should now look like the following screenshot:

Key binding

You will notice that none of the buttons work in the actual menu so we cannot exit the pause menu or view any of the submenus. This is because we have not programmed any of the menu buttons yet. We will now focus on programming those buttons.

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

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