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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
How to Create a Simple Game in Unreal Engine 4
30 mins
- Getting Started
- Moving the Player Forward
- Moving Along a Single Axis
- Creating the Tunnel Spawner
- Testing the Tunnel Spawner
- Setting up the Tunnel Blueprint
- Creating the Trigger Zone
- Creating the Spawn Point
- Spawning Tunnels at the Spawn Point
- Creating a Reference to the Tunnel Spawner
- Scripting the Trigger Zone
- Spawning More Tunnels
- Spawning the First Tunnel
- Spawning Subsequent Tunnels
- Creating Obstacles
- Creating Wall Variations
- Randomizing the Wall
- Handling Wall Collisions
- Setting the IsDead Variable
- Displaying a Restart Button
- Creating the Display Function
- Calling the Display Function
- Restarting the Game
- Resetting the Player
- Respawning the Tunnels
- Handling Button Clicks
- Where to Go From Here?
Creating the Display Function
Create a new function and name it DisplayRestart. Once you have done that, create the following graph:
Summary:
- Add to Viewport will show RestartWidget on the screen
- 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.
- 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:
- Reset the player. This includes removing the restart button from the screen.
- 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:
- Set Is Dead sets IsDead to false. This re-enables forward movement.
- Remove From Parent removes RestartWidget from the screen
- Set Input Mode Game Only re-enables game input so the player can move around
- 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.
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:
- Get Owning Player Pawn returns the Pawn that the player is currently controlling
- Cast to BP_Player checks if the Pawn is of class BP_Player
- If it is, it will call the RestartGame function. This function resets the player and hides the restart button.
- Get All Actors of Class and Get returns BP_TunnelSpawner and then calls SpawnInitialTunnels. This function will remove existing tunnels and spawn new ones.
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.