Book Image

Blueprints Visual Scripting for Unreal Engine

By : Brenden Sewell
Book Image

Blueprints Visual Scripting for Unreal Engine

By: Brenden Sewell

Overview of this book

Blueprints Visual Scripting for Unreal Engine is a step-by-step approach to building a fully functional game, one system at a time. Starting with a basic First Person Shooter template, each chapter will extend the prototype to create an increasingly complex and robust game experience. You will progress from creating basic shooting mechanics to gradually more complex systems that will generate user interface elements and intelligent enemy behavior. Focusing on universally applicable skills, the expertise you will develop in utilizing Blueprints can translate to other types of genres. By the time you finish the book, you will have a fully functional First Person Shooter game and the skills necessary to expand on the game to develop an entertaining, memorable experience for your players. From making customizations to player movement to creating new AI and game mechanics from scratch, you will discover everything you need to know to get started with game development using Blueprints and Unreal Engine 4.
Table of Contents (15 chapters)
Blueprints Visual Scripting for Unreal Engine
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Changing direction


If you were to compile the Blueprint, save, and play the game now, what would you expect to see? The target cylinder would move according to our speed and direction as soon as the game began. However, since we don't have any instructions that cause the target to stop moving, it would proceed in the same direction for as long as the game runs, even moving through objects and out of the level we created! To address this, we need logic that will change the target's direction periodically. This will result in a target that moves back and forth between two points regularly, much like a shooting gallery target.

To do this, we have to set up two nodes that will set the direction variable we created to two different values. Drag the direction variable into empty grid space and choose the Set option. This will result in a node with X, Y, and Z axis fields. We can use them to change the value of the direction variable to be different from the initial default value that we gave it. We want two of these nodes, so drag the direction variable again into empty space, and then change the Y axis values of the two nodes to 10.0 and -10.0 respectively.

Now we need a way to switch between these two nodes so that the direction repeatedly shifts. The FlipFlop node was created for scenarios where we know we want to alternate between two sets of actions that execute exactly once before switching each time. This fits our use case here, so right-click on empty grid space and search for FlipFlop. Select and place the node. Then connect the A execution pin to one of the direction set node input pins, and the B execution pin to the other.

Finally, we need to ensure that there is some kind of delay between executing the direction shifts. Otherwise, the direction will change for every frame and the object will go nowhere. To do so, drag the input execution pin of the FlipFlop node into empty space and search for the Delay node. This node allows us to set a delay duration, in seconds, that will postpone the following execution commands by that length of time. Place this node before the FlipFlop node and give it a duration of 6 seconds. Placing this Delay node between our Set Actor Transform node and our FlipFlop node will ensure that the direction switch enabled by FlipFlop will occur only every 6 seconds. The final product should look like what is shown in the following screenshot. Once you are done, remember to compile and save the Blueprint.

Testing moving targets

Now that we have our Blueprint updated, we can test to ensure that the CylinderTarget object moves as expected. First, we will have to place the CylinderTarget object in a position that will allow it to move along the Y axis without bumping into other objects. The coordinates I used were 410 on the X axis, 680 on the Y axis, and 180 on the Z axis.

Note that these values will only work relative to the default layout of the First Person template map. If you have made adjustments to your own level, then you can adjust either the speed or the placement of the target in your level, and test until you find a good patrol spot. Click on Play. If the Blueprint is functioning correctly, you will see the cylinder move back and forth between two points at a steady rate.

One of the advantages of using Blueprints is that they create a template of functionality that can be used across multiple objects in a scene. Find CylinderTarget_Blueprint in the Blueprints folder and drag it directly onto 3D Viewport. You will see another cylinder created, which inherits all of the functionality of our original cylinder target. In this way, we can rapidly set up multiple moving targets using the single set of Blueprint logic we created.