Adding to an existing Blueprint – flashlight, part 2

Now that we have some experience working with Blueprints, let's modify one that's already been created for us. In this section, we will cover how to add a flashlight to our game's character.

Getting ready

Before we start working within the Unreal Editor, we will need to have a project to work with. Follow these steps:

  1. First, open up the Unreal Editor by clicking on the Launch button from the Unreal Engine Launcher.
  2. Start a new project from the Project Browser tab by selecting the New Project tab. Select First person and make sure that With Starter Content is selected. Give the project a Name (Cookbook_Chapter8). Once you are done, click on Create Project.
  3. After the project is opened, go to the Example Code folder and drag and drop the NightScene map provided in your project folder and open it:
    Getting ready

    At this point, if you were to play the game, it would be incredibly dark, as shown in the following screenshot:

    Getting ready

How to do it…

For those who have gone through Chapter 6, Lighting and Shadows, this should look quite familiar. In that chapter, we actually created a flashlight and then attached it to our player at runtime.

However, we can modify the character's blueprint to make it so that our player will always have a flashlight on. Follow these steps:

  1. Go to the Content Browser tab and then to the Blueprints folder. Find the MyCharacter file.
    How to do it…

    Selecting the My Character blueprint

    This blueprint is what's spawned when the game is started. It contains all of the logic for movement, the camera, and the shooting behaviour.

  2. Next, double-click on the file to open up the Blueprints for it. Initially, it will be on the Event Graph tab, which will have a lot of scripts on it, very similar to a level blueprint. But for now, click on the Viewport tab and zoom the camera out if needed.
    How to do it…

    The Viewport tab view in the blueprint editor

    This section is similar to our level in that it has objects and they're placed in the world of our object, but it's apart from the level. You can move around the viewport on the right-hand side in exactly the same way as the main game. Zoom out so that you can see everything.

  3. To the left, you'll see a tab marked Components. Under this, it has the mesh, camera, and everything else that is to do with the physical presence of the class. We can add to this using the Add Component option. With this in mind, click on Add Component and select Spot Light (if you can't see it, use the search bar at the top).
    How to do it…

    Adding a Spot Light to our character

    You can see that the spotlight is already added for us.

  4. Go back into the main editor and play the game again.
    How to do it…

    You'll notice a couple of issues. First of all, the light is lighting the player in addition to what's in front of it. Also, if we look up and down, it does not move with us.

  5. With the game still playing, go back to the MyCharacter blueprint and select our SpotLight object and move it (if the Move tool isn't there, press W). Notice that when we move it in the blueprint, it modifies what is done in the game. Move the light until it is in front of the barrel of the gun.
    How to do it…

    Moving the spot light in game

    This solves the issue of lighting the player, but moving up and down is not working. To do this, we can make the SpotLight object a child of the camera. That way, whenever it moves, the light will move as well. Sadly, we cannot change this while the game is running.

  6. Stop the game and then drag and drop the SpotLight object on top of the FirstPersonCamera object. You should see a green checkmark, saying that we can attach it.
    How to do it…

    Attaching spotlight to the FirstPersonCamera object

  7. After this, go into the SpotLight objects and set the same parameters that we did in the Adding moveable lights – flashlight, part 1 recipe of Chapter 6, Lighting and Shadows or translate the object so that it is in front of the camera with Inner Cone Angle of 8 and Outer Cone Angle of 10.
  8. Click on the Compile button to save all the changes we've made before and confirm that it all works correctly with our project.
  9. Lastly, let's add in the ability to turn the flashlight on and off using an input event. Click on Event Graph and move it over until you have some empty space.

    Tip

    While in Event Graph, it may be beneficial to take a look at the actions that are actually part of the player class, giving you an idea of what can be done with blueprints and other actions that you can use!

  10. Right-click on the empty space and type in keyboard events. This will display all of the keyboard keys and allow us to trigger events based on when they are pressed or released.
  11. Select F, and you'll see an event is created. To the right of it, right-click and create a Toggle Visibility (SpotLight1) action. Toggle means that it will turn it off if it's on and on if it's off.

    Tip

    If you want to specifically set a property, use the Set Visibility option using a variable to tell what it should do.

  12. Next, connect the Pressed output of the F event to the input of the Toggle Visibility action.
    How to do it…

    Connecting the Pressed output to the input of the Toggle Visibility action

  13. Compile, go to the Content Browser tab, save the MyCharacter blueprint, and Play the game!
    How to do it…

With this, we now have a flashlight that will follow our player's camera in all directions and exist in every single level we have in the game! Here, you learned how to modify the already existing blueprints and added in some simple player input using the F key to toggle visibility.

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

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