The Next feature to add was a pause menu. I used a couple Virtus's YouTube videos for reference on the nodes that I should use, but generally a lot of the blueprint functionality is similar to what I've used previously. I will go through steps involved in getting it working.
Firstly, a key binding needs to be created, basically setting up an input that will do something in the game. To do this, go to Edit > Project Settings. Scroll down the list on the left until you find the 'Input' option. Now click on Bindings > Action Mappings and hit the '+' to create a new one, I called mine Pause. Next you need to select what key (or controller button) will pause the game, I used the escape button as it generally is the pause key in most games.
Now right click in the content bowser, select User Interface and create a new widget blueprint, name and open it. From the Palette on the left, drag and drop in two buttons, these will be the pause and quit buttons, you can rename the buttons in the details panel on the right or the Hierarchy panel on the left. To finish the buttons off, drag and drop 'Text' from the palette on to the buttons, and enter appropriate names in the details panel and press compile.
Next open the First Person Character blueprint, or whatever is the main character blueprint you are using. Go to the Event Graph tab and find a space to create new nodes. First we need to call an event when the escape key is pressed > a branch is needed to check a Boolean variable, because we only want to have one instance of the pause menu open. If the Boolean is false > we move to create widget, with the class set to the pause menu widget > now we add the widget to the viewport (screen) > as the pause menu is now open, the pause variable needs to be set to true > the game is now set to paused > finally the mouse pointer is set to visible, the target is the Player Controller. Click compile.
Now we just need to add some functionality to the those buttons. Return back to the pause menu blueprint and select 'Graph' in the top right corner. On the right you'll notice that buttons you created now appear as variables. Select the quit button, and scroll down to events and add the on the click event. A new node will appear 'OnClicked(Quit)', we just need this to return to the start screen (or quit the game).
Next is the resume button. From the clicked on event > game paused is set to false > the pause screen widget is removed > we need to cast to the first person character to gain access to the pause active variable > and set the mouse pointer to false. The last step is to make the pause menu close with the escape key. Starting from Event Tick > we add in a slight delay > this leads to branch which is checking every 0.2 of a second if the escape is being pressed > if it is, game paused is set to false.
Next is the resume button. From the clicked on event > game paused is set to false > the pause screen widget is removed > we need to cast to the first person character to gain access to the pause active variable > and set the mouse pointer to false. The last step is to make the pause menu close with the escape key. Starting from Event Tick > we add in a slight delay > this leads to branch which is checking every 0.2 of a second if the escape is being pressed > if it is, game paused is set to false.
No comments:
Post a Comment