Create A Parasite Platformer Game In Unreal Engine Part 8: Reaching The Goal In The Game And Dealing Damage To The Player

Table of Contents

Create A Parasite Platformer Game In Unreal Engine Part 8: Reaching The Goal In The Game And Dealing Damage To The Player

Reading Time: 10 minutes
Level: Beginner
Version: Unreal Engine 4.26

Help Others Learn Game Development

In part 7 of this tutorial series we learned how to work with UI widgets, how to create user interfaces and how to spawn them in the game.

In this part of the tutorial we are going to create the code logic that will be triggered when the PlayerCharacter reaches the goal of the level and when the PlayerCharacter collides with the Fire Obstacle.

Creating Functions In Blueprints

We saw that we can use functions to group blocks of code so that we can reuse it.
 
We can do the same thing in Blueprints only here we are grouping a block of nodes that form the code that will be executed when the function is called.
 
Open the Blueprint editor for BP_ParasitePlatformer_GameMode and in the My Blueprint tab you will see that we have lists where we can store functions:
 
Img 1

Same as how we create variables we can create functions by clicking on the + button under the Functions list:

Img 2

And same as how you can give a name to a variable you can also give a name to a function. So name the newly created function to Create Game Over UI.

I will also add that all the rules and ways about renaming variables also apply to functions as well.

One important thing to know when you are editing a function is that it opens in a new tab inside the editor:

As you can see from the preview you can close the tab of the function and you can open it again when you need it by double clicking on the function inside the My Blueprint tab.
 
The same goes for the Event Graph which is also a tab and we demonstrated that you can close it and open it.
 
This is important to know since we will be working a lot with functions and we will be closing and opening tabs for different functions all the time.
 

Create Game Over UI Function

Now that we know how to create a function, let’s go inside the Create Game Over UI function and add the code that we need.
 
The code is basically going to be the same as the one we used in the previous tutorial so if you need a refresher how this works you can click here.
 
We are going to call the Create Widget node and select our BP_GameOver_UI as the widget that we will create.
 
After that we will simply add it to the view port by calling Add To Viewport node:
 
Img 3 - Node Code

You can copy the nodes from here:


Detecting Collision Between Actors

If we try to pass through the Doorway that we created with our PlayerCharacter Actor, nothing will happen:

As you can see we pass through the Doorway and we can even fall down on the other side of it and nothing changes in the game in terms of informing the user that we reached the level goal.

To fix this, we need to open the BP_Door in the Blueprint editor.

In the Event Graph, you can delete all the nodes that we have by default because we will not use them.

Since we need to detect collision using the Door Collision component, we are going to call On Component Begin Overlap event of that component.

To do that, select the Door Collision component in the Components tab, then inside the Details tab scroll all the way to the bottom until you reach the Events settings and from there, select the On Component Begin Overlap event:

When we locate the event we want to use, click on the green + button to the right of that event to create it as a node in the editor, same as what you saw in the video above.

The On Component Begin Overlap event will inform us every time an Actor colliders with the Door Collision component.

From the On Component Begin Overlap event we have parameters that we can use to test who is the Actor that has collided with the Door Collision.

To test if the PlayerCharacter has collided with the Door Collision we are going to cast the Other Actor parameter to BP_PlayerCharacter:

Img 4 - Node Code
You can copy the nodes from here:
 

We already talked about casting and what it means. If you need to refresh you knowledge you can click here.

Since the Cast To node takes a parameter an Object that will be casted, we are passing the Other Actor from the On Component Begin Overlap.

The Other Actor represents the other actor who was collided with the specific component, in our case the Door Collision component.

So if the PlayerCharacter Actor collides with the Door Collision, we are going to call the Create Game Over UI function that we created inside the BP_ParasitePlatformer_GameMode:

Img 5 - Node Code

You can copy the nodes from here:

As you can see from the nodes above, if the cast to PlayerCharacter succeeds, then we are going to get the BP_ParasitePlatformer_GameMode by casting the returning value of the Get Game Mode function.

If that cast succeeds, simply call the Create Game Over UI which is the function that we previously created inside the BP_ParasitePlatformer_GameMode.

Now we can test the game:

Now when we collide with the Door Collision, we will create the UI to display to the user that the game is over.

Set The Input Control To UI Widget

While we are displaying game over UI when we reach the Doorway, we have two problems.

First, if we press the Play Again button nothing will happen, and second, even though game over UI is displaying on the screen we can still move in the game.

To be able to press the button in the game over UI, we need to call a function called Set Input Mode UI Only.

Inside the Create Game Over UI function in the BP_ParasitePlatformer_GameMode, after we add the game over UI to the viewport call the Set Input Mode UI Only node:

Img 6 - Node Code
You can copy the nodes from here:
 
If you can’t locate the Set Input Mode UI Only function make sure that you turn off Context Sensitive when you are searching for the node, you can refresh your knowledge about that topic by clicking here.
 
The Set Input Mode UI Only function takes a few parameters, one of them is the Player Controller which you can find by Right Click and then filter for get player controller in the search bar.
 
The function will return the Player Controller that we set in the Project Settings -> Maps & Modes, or it will return the default one if we didn’t set a custom Player Controller.
 
The Set Input Mode UI Only function also takes a parameter a widget that will be focused e.g. the widget where the input mode will be set, in our case the game over UI.
 
To do this we use the return value of the Create Widget function:
 


Organizing The Nodes In The Blueprint Editor

The node code above will give the input control to the game over UI when it is created, so that issue is fixed.

However, I want to mention one thing that is concerning the organization of the nodes that we call.

The game we are creating is a small game where we don’t have too many nodes in our Blueprints.

But when we start working on more complex games the nodes start to clutter up and we can easily mix up the lines connecting the nodes and it quickly gets to a messy node spaghetti code.

How can we fix this?

Well we can bend the lines connecting the nodes by adding additional connecting dots on the line itself.

This can be done by double clicking on the line, and we can also delete these lines by selecting them and pressing the delete button on the keyboard:

When we add these extra dots from where we can drag the lines to connect the nodes we can easily organize them and not make them obscure one another.

This will be really useful when we start working on more complex games where we will need to organize the nodes like this to avoid confusion and the already mentioned node spaghetti code.

Pausing The Game

Now that we are giving the input control to the UI widget, we also need to make sure that our mouse cursor is visible and that the game is paused:

Img 7 - Node Code

You can copy the nodes from here:

The Show Mouse cursor node takes a parameter of Player Controller, so we passed the return value of the Get Player Controller function.

Also make sure that you check the checkbox for the Show Mouse Cursor bool to denote that we should show the mouse cursor.

For the Set Game Paused function we need to do the same and check the Paused checkbox denoting that it is true e.g. we should pause the game.

Compile and Save the changes and run the game:

Now when we reach the level goal, the game is paused, the PlayerCharacter can’t move, and we can press the Play Again button.

Restarting The Game When We Press The Button

While we can press the Play Again button now, nothing happens because we didn’t set any code to execute when the button is pressed.

To change this, go inside the BP_GameOver_UI Blueprint. Select the Play Again Button from the Hierarchy tab and in the Details tab scroll to the bottom where the Events settings is and select the On Clicked event:

Img 8

When you click on the On Clicked event it will take you to the Graph editor inside the BP_GameOver_UI Blueprint.

Remember that you can always switch back and forth from the Graph editor where we place nodes, and the Designer editor where we place the UI widgets by click on the tabs located at the top right corner:

Img 9

So when we press the Play Game Button we will restart the level:

Img 10

You can copy the nodes from here:

The Get Current Level name function will return the name of the current level we are in, and the Open Level (by Name) function will open the level with the given name.

We only need to pass the returning value of the Get Current Level Name function to the Level Name parameter of the Open Level (by Name) function.

Just be careful when you are calling the Open Level function because there are two versions of it. One will open the level by name and the other will open the level by object reference.

So make sure you select the Open Level (by Name):

Img 11

Compile and Save and run the game:

Now when we press the Play Again Button we restart the game and the PlayerCharacter is spawned where the Player Start Actor is placed.

However, we have one problem, when the game is restarted we can’t move the PlayerCharacter which I am sure you noticed when you pressed the buttons on your keyboard or when you tried to use the mouse to rotate him.

Unpausing The Game And Giving Back Input Control To The Game

When we create the game over UI we give the input control to it and we pause game.
 
This behavior persists even when we restart the game and that is the reason why we can’t move the PlayerCharacter when the game is restarted.
 
To be able to move the PlayerCharacter we need to unpause the game and give back input control to the game.
 
Inside the Graph editor for the BP_GameOver_UI after we open the level add the following nodes:
 
Img 12
You can copy the nodes from here:
 
Again we need to get a reference to the Player Controller because we need to pass him as a parameter to the Set Input Mode Game Only function which will return the input control back to the game and we will be able to control the PlayerCharacter.
 
After that we simply call Set Game Paused but this time we make sure that the Paused checkbox is unchecked.
 
Compile, Save and run the game:
 

Now when we restart the game, we can move the PlayerCharacter.

Dealing Damage To The Player

Now that we have the level goal part out of the way, let us make the Fire Obstacle deal damage to the PlayerCharacter.

I said make the Fire Obstacle deal damage to the PlayerCharacter but actually the Fire Obstacle is going to kill the PlayerCharacter as soon as it touches him which will make the game a lot more challenging than adding a health variable and subtracting the damage from it.
 
To do this, open the BP_MovingObstacle_Fire in the Blueprint editor.
 
Add the following nodes from the ActorBeginOverlap event node:
 
Img 13
You can copy the nodes from here:
 
Essentially we are testing if we collide with the PlayerCharacter e.g. if the Fire Sphere Actor collides with the PlayerCharacter.
 
If that is true, then we get a reference to the BP_ParasitePlatformer_GameMode by casting the returning value of the Get Game Mode function, an example we already saw here.
 
When we get a reference to the BP_ParasitePlatformer_GameMode we call the Create Game Over UI function to inform the user that the game is over and he has died in the game.
 

Now when we collide with the Fire Obstacle we should see the game over UI. Let’s the test the game:

As we can see from the preview above, when we collide with the Fire Obstacle nothing happens.

What is the issue?

The issue is that the Collision Preset for the Mesh component inside the BP_MovingObstacle_Fire Blueprint is set to BlockAllDynamic.

You can locate this by going inside the Blueprint editor, select the Mesh component and inside the Details tab scroll to the Collision settings:

Img 14

This setting will block all collisions with the Fire Obstacle. To fix this, click on the drop down list and select the OverlapAllDynamic collision preset:

Img 15

The OverlapAllDynamic preset will enable collision between all Actors e.g. any Actor that collides with the Fire Obstacle that collision will be detected.

Let’s test the game now:

Now when we collide with the Fire Obstacle the collision works and we see the game over UI on the screen.

Where To Go From Here

In this tutorial we created the logic that will end the game when the PlayerCharacter either reaches the goal of the level or if he collides with the Fire Obstacle.
 
This same method will be used when the PlayerCharacter collides with the Parasite Monster.
 
In the next part of this tutorial series titled Importing And Animating The Parasite Enemy you will learn to work with animations in Unreal Engine.
 

Leave a Comment