Setting up a lose screen

The lose screen will be presented when the player runs out of health. We will present them with options to restart the last round or quit the game. You may remember the win screen we created; we presented similar options there. Rather than remaking the UI screen from scratch, we can save some time by using our WinMenu object as a template.

Go to Content Browser and open the UI folder inside FirstPersonBp. Right-click on WinMenu and select the Duplicate option. Name this new Blueprint Widget LoseMenu. Now, open LoseMenu and select the text object showing You Win. Look at the Details panel and change the Text field under Content to You Lose. Try Again?. Also, change the Color and Opacity setting to a dark red color. Finally, you may wish to change the Shadow Color setting's alpha value A from 0.0 to 1.0 in order to show shadows behind the text, as shown in this screenshot:

The two buttons, Restart and Quit, can remain identical to their WinMenu counterparts in appearance and functionality for now. Compile and save this Blueprint, and then return to the Content Browser. Open FirstPersonCharacter inside the Blueprints folder.

To track whether or not the player has lost the game, we will need to create a new variable. From the My Blueprint panel, add a new variable called Lost Game. In the Details panel, set Variable Type to Boolean. With the variable created, find the series of nodes we used to decrease the player's health, triggering the Event Any Damage node.

We will need to extend the SET Player Health operation with a Branch comparison that will test whether the player's health is less than zero, and if so, set the Lost Game variable to True and end the game. The nodes used to accomplish this are shown in the following screenshot:

Begin by giving yourself additional room to the right of the SET Player Health node. Drag a wire from the output execution pin of this node to a Branch node. Then, connect the Condition input pin of the Branch node to a float <= float node. Leave the bottom input field of this new node as 0.0, and then connect the top input pin to the output pin of the SET Player Health node.

Now, drag a wire from the True output execution pin of the Branch node, and then connect it to a SET Lost Game node. Check the Lost Game input pin box to set this Boolean to True when the player runs out of health. Then, connect the SET Lost Game node to an End Game node, which will call our previously created Function to show WinMenu. The next step will be to edit the End Game Function so that it will show LoseMenu if the Function is called while the Lost Game variable is set to True.

Find the block of nodes that triggers the End Game Event, where we call our WinMenu screen. After the nodes that pause the game and enable the mouse arrow, we are going to create a branch node that will test the Lost Game variable, as seen in the following screenshot:

Start by breaking the connection between the SET Show Mouse Cursor and Create WinMenu_C Widget nodes, and then drag the Widget and Viewport nodes to the side for now. Then, connect the Set Show Mouse Cursor node to a new Branch node. Next, drag the Lost Game variable onto the Condition input pin of the Branch node. The next step will be to create and display LoseMenu when Lost Game is True, and WinMenu when it is False, as seen in this screenshot:

Drag a wire from the True output execution pin of the Branch node, and then connect it to a new Create Widget node. Inside this node, select LoseMenu from the Class input drop-down menu. Then, drag a wire from the Return Value output pin to an Add to Viewport node. Finish the True branch by attaching the Viewport node to a SET Lost Game node, and ensure that the Lost Game input checkbox is left unchecked. This step is necessary to ensure that the game won't mistakenly think that the player has already lost if they restart or resume playing later. Finally, reconnect the create Widget and Viewport nodes you dragged aside earlier to the False output execution pin of the Branch node.

Compile, save, and then click on Play to test your work. If you stand next to an enemy long enough for it to drain your health to zero, then you should now see LoseMenu we created.

Now, the player needs to be more careful with the enemies, so as to not lose the game. The next step to make the game more interesting is to create a round-based experience.

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

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