How to Create a Simple Game in Unreal Engine 4

In this Unreal Engine 4 tutorial, you will create a first-person endless game. You will learn how to generate random obstacles and restart the game. By Tommy Tran.

4.1 (49) · 1 Review

Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Creating the Display Function

Create a new function and name it DisplayRestart. Once you have done that, create the following graph:

Summary:

  1. Add to Viewport will show RestartWidget on the screen
  2. Set Input Mode UI Only will limit the player’s interactions to the UI. This is so the player can’t move around while they are dead.
  3. As its name suggets, Set Show Mouse Cursor simply displays the mouse cursor

To display the restart button, all you need to do is call DisplayRestart after the player collides with a wall.

Calling the Display Function

Close the DisplayRestart graph and then click Compile.

Switch to BP_Tunnel and then locate the On Component Hit (WallMesh) node.

Add a DisplayRestart node to the end of the node chain.

Click Compile and then close BP_Tunnel. Go back to the main editor and press Play. If you hit a wall, the restart button will appear.

The last step is to restart the game when the player clicks the button.

Restarting the Game

There are two things the game needs to do when restarting:

  1. Reset the player. This includes removing the restart button from the screen.
  2. Respawn the tunnels. This is so the player starts at the beginning of the tunnel.

Let’s start with resetting the player.

Resetting the Player

Open BP_Player and then create a new function called RestartGame. Create the following graph:

Summary:

  1. Set Is Dead sets IsDead to false. This re-enables forward movement.
  2. Remove From Parent removes RestartWidget from the screen
  3. Set Input Mode Game Only re-enables game input so the player can move around
  4. Set Show Mouse Cursor hides the mouse cursor

Next, let’s respawn the tunnels.

Respawning the Tunnels

Click Compile and then close BP_Player.

Open BP_TunnelSpawner and make sure you are in the SpawnInitialTunnels graph.

First, you need to remove the existing tunnels before you spawn new ones.

Add a Sequence node after the Entry node. Connect the Then 1 pin to the ForLoop node.

Note: The Sequence node executes its outputs in sequential order. It is a great way to organize your graph vertically especially since node chains can get very long.

Next, create the following nodes:

This setup will get all the existing tunnels and remove them from the game.

Finally, connect the Then 0 pin of the Sequence node to the Get All Actors of Class node. This will make sure the tunnels are removed before the spawning process.

Here is the final graph:

The last thing to do is handle the button click.

Handling Button Clicks

Click Compile and then close BP_TunnelSpawner.

Go to the Content Browser and navigate to the UI folder. Double-click on WBP_Restart to open it.

Select RestartButton and then go to the Details panel. Go to the Events section and click the button next to OnClicked.

This will create a node called On Clicked (RestartButton). This node will execute when the player clicks on RestartButton.

Recreate the following:

Summary:

  1. Get Owning Player Pawn returns the Pawn that the player is currently controlling
  2. Cast to BP_Player checks if the Pawn is of class BP_Player
  3. If it is, it will call the RestartGame function. This function resets the player and hides the restart button.
  4. Get All Actors of Class and Get returns BP_TunnelSpawner and then calls SpawnInitialTunnels. This function will remove existing tunnels and spawn new ones.
Note: You might be wondering why I used Get All Actors Of Class instead of using a reference to BP_TunnelSpawner. The main reason is because BP_Tunnel doesn’t have a relationship to WBP_Restart. For a simple game like this, it is easier to do the above method rather than figuring out where to store a reference.

Click Compile and then close the Blueprint editor. Press Play to test out the restart button!

Where to Go From Here?

You can download the completed project here.

Now that you have a simple game, you can start building on it. Try adding a score counter that increases when the player avoids a wall.

You should also try to build classic games like Pong and Tetris. The mechanics of these games are simple but can be a challenge to implement.

If you’d like to learn how to make a specific type of game, leave a comment below!

If you want to keep learning, check out the next post in the series, where I’ll show you how to animate characters in your game using Blueprints.

Tommy Tran

Contributors

Tommy Tran

Author

Over 300 content creators. Join our team.