Blueprints Visual Scripting For Unreal Engine - Sample Chapter
Blueprints Visual Scripting For Unreal Engine - Sample Chapter
$ 29.99 US
19.99 UK
"Community
Experience
Distilled"
Brenden Sewell
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Brenden Sewell
years designing and creating games that are both fun to play and have educational or
social impact. He has been building games since 2002, when Neverwinter Nights taught
him an invaluable lesson about the expressive power of game design. In 2010, he
graduated with a degree in cognitive science from Indiana University. Since then, he
has focused on enhancing his own craft of game design while harnessing its power to
do good in the world, and exposing more people to the joy the profession holds.
Preface
Game engines, such as Unreal Engine 4as the tools that power the creation of
the commercial games we love to playare becoming increasingly accessible to
both experienced and novice game developers outside of the traditional studio
environment. Previous versions of Unreal Engine have powered many of the most
popular console and PC games released over the last decade, and the newest version
contains the tools for funneling this power into the hands of as many aspiring
developers as possible. The most transformative of these tools is the Blueprints
Visual Scripting system, which allows people who are not full-time programmers
to create and implement the mechanics, interfaces, and interactions of a game.
Taking a step-by-step approach, this book will guide you through the process of
using the visual nodes that make up Blueprint behavior, and link them together
to create game mechanics, user interfaces, and more. In this process, you will be
learning all the skills you need to get started with developing games in Unreal
Engine 4 using Blueprints.
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. By 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 this book, you will have a
fully functional first-person shooter and the skills necessary to expand on the game to
develop an entertaining, memorable experience for your players.
Preface
Object Interaction
with Blueprints
When setting out to develop a game, one of the first steps toward exploring your
idea is to build a prototype. Fortunately, Unreal Engine 4 and Blueprints make it
easier than ever to quickly get the essential gameplay functionality working so that
you can start testing your ideas sooner. To develop some familiarity with the Unreal
editor and Blueprints, we will begin by prototyping simple gameplay mechanics
using some default assets and a couple of Blueprints.
In this chapter, we will cover the following topics:
The Library tab is the location where you will be able to access the versions of the
engine you have installed and the projects you have built. Now click on the Library
tab, find the yellow Install button (as seen in the following screenshot), and click
on it:
When the engine has finished installing, the Install buttons will change to Launch
buttons, as shown in the following screenshot. Click on any of the Launch buttons
to open the engine.
[2]
Chapter 1
[3]
[4]
Chapter 1
Exploring materials
Earlier, we set for ourselves the goal of changing the color of the cylinder when it is
hit by a projectile. To do so, we will need to change the actor's material. A material is
an asset that can be added to an actor's mesh (which defines the physical shape of the
actor) to create its look. You can think of a material as paint applied on top of an actor's
mesh or shape. Since an actor's material determines its color, one method of changing
the color of an actor is to replace its material with a material of a different color. To do
this, we will first be creating a material of our own. It will make an actor appear red.
[5]
Creating materials
We can start by creating a new folder inside the FirstPersonBP directory and
calling it Materials. Navigate to the newly created folder and right-click inside
empty space in the content browser, which will generate a new asset creation popup.
From here, select Material to create a new material asset. You will be prompted to
give the new material a name, which I have chosen to call TargetRed.
[6]
Chapter 1
You are now looking at Material Editor, which shares many features and
conventions with Blueprints. The center of this screen is called the grid, and this is
where we will place all the objects that will define the logic of our Blueprints. The
initial object you see in the center of the grid screen, labeled with the name of the
material, is called a node. This node, as seen in the previous screenshot, has a series
of input pins that other material nodes can attach to in order to define its properties.
To give the material a color, we will need to create a node that will give information
about the color to the input labeled Base Color on this node. To do so, right-click on
empty space near the node. A popup will appear, with a search box and a long list
of expandable options. This shows all the available Blueprint node options that we
can add to this Material Blueprint. The search box is context sensitive, so if you start
typing the first few letters of a valid node name, you will see the list below shrink
to include only those nodes that include those letters in the name. The node we are
looking for is called VectorParameter, so we start typing this name in the search
box and click on the VectorParameter result to add that node to our grid:
A vector parameter in the Material Editor allows us to define a color, which we can
then attach to the Base Color input on the tall material definition node. We first need
to give the node a color selection. Double-click on the black square in the middle of
the node to open Color Picker. We want to give our target a bright red color when
it is hit, so either drag the center point in the color wheel to the red section of the
wheel, or fill in the RGB or Hex values manually. When you have selected the shade
of red you want to use, click on OK. You will notice that the black box in your vector
parameter node has now turned red.
Downloading the example code
You can download the example code fies from your account at
http://www.packtpub.com for all the Packt Publishing books you
have purchased. If you purchased this book elsewhere, you can visit
http://www.packtpub.com/support and register to have the fies
e-mailed directly to you.
[7]
To help ourselves remember what parameter or property of the material our vector
parameter will be defining, we should name it color. You can do this by ensuring
that you have the vector parameter node selected (indicated by a thin yellow
highlight around the node), and then navigating to the Details panel in the editor.
Enter a value for Parameter Name, and the node label will change automatically:
The final step is to link our color vector parameter node to the base material node.
With Blueprints, you can connect two nodes by clicking and dragging one output pin
to another node's input pin. Input pins are located on the left-hand side of a node,
while output pins are always located to the right. The thin line that connects two
nodes that have been connected in this way is called a wire. For our material, we
need to click and drag a wire from the top output pin of the color node to the Base
Color input pin of the material node, as shown in the following screenshot:
[8]
Chapter 1
Once you have a scalar parameter node, select it, and go to the Details panel. A scalar
parameter takes a single float value (a number with decimal values). Set Default Value
to 0.1, as we want any additive effects to our material to be subtle. We should also
change Parameter Name to Metallic. Finally, we click and drag the output pin from
our Metallic node to the Metallic input pin of the material definition node.
We want to make an additional connection to the Roughness parameter, so
right-click on the Metallic node we just created and select Duplicate. This will
generate a copy of that node, without the wire connection. Select this duplicate
Metallic node and then change the Parameter Name field in the Details panel to
Roughness. We will keep the same default value of 0.1 for this node. Now click and
drag the output pin from the Roughness node to the Roughness input pin of the
Material definition node.
[9]
The final result of our Material Blueprint should look like what is shown in the
following screenshot:
We have now made a shiny red material. It will ensure that our targets will stand out
when they are hit. Click on the Save button in the top-left corner of the editor to save
the asset, and click again on the tab labeled FirstPersonExampleMap to return to
your level.
Chapter 1
For this project, we will be storing all our Blueprints in the Blueprints folder, inside
the FirstPersonBP folder. Since this is the Blueprint for our CylinderTarget actor,
leaving the name of the Blueprint as the default, CylinderTarget_Blueprint,
is appropriate.
[ 11 ]
[ 12 ]
Chapter 1
We want to trigger a "change material" action on our target every time a projectile
hits it. While we could do this by utilizing the Event Actor Begin Overlap node to
detect when a projectile object was overlapping with the cylinder mesh of our target,
we will simplify things by detecting only when another actor has hit our target actor.
Let's start with a clean slate, by clicking and dragging a selection box over all the
default events and hitting the Delete key on the keyboard to delete them.
Detecting a hit
To create our hit detection event, right-click on empty graph space and type hit in
the search box. The Event Hit node is what we are looking for, so select it when it
appears in the search results. Event Hit triggers actions every time another actor
hits the actor controlled by this Blueprint.
Once you have the Event Hit node on the graph, you will notice that Event Hit
has a number of multicolored output pins originating from it. The first thing to
notice is the white triangle pin that is in the top-right corner of the node. This is the
execution pin, which determines the next action to be taken in a sequence. Linking
the execution pins of different nodes together enables the basic functionality of all
Blueprints. Now that we have the trigger, we need to find an action that will enable
us to change the material of an actor. Click and drag a wire from the execution pin
to empty space to the right of the node.
Dropping a wire into empty space like this will generate a search window, allowing
you to create a node and attach it to the pin you are dragging from in a single
operation. In the search window that appears, make sure that the Context Sensitive
box is checked. This will limit the results in the search window to only those nodes
that can actually be attached to the pin you dragged to generate the search window.
With Context Sensitive checked, type set material in the search box. The node we
want to select is called Set Material (StaticMeshComponent).
[ 13 ]
If you cannot find the node you are searching for in the contextsensitive search, try unchecking Context Sensitive to find it from the
complete list of node options. Even if the node is not found in the
context-sensitive search, there is still a possibility that the node can be
used in conjunction with the node you are attempting to attach it to.
The actions in the Event Hit node can be set like this:
Swapping a material
Once you have placed the Set Material node, you will notice that it is already
connected via its input execution pin to the Event Hit node's output execution pin.
This Blueprint will now fire the Set Material action whenever the Blueprint's actor
hits another actor. However, we haven't yet set up the material that will be called
when the Set Material action is called. Without setting the material, the action will
fire but not produce any observable effect on the cylinder target.
[ 14 ]
Chapter 1
To set the material that will be called, click on the drop-down field labeled Select
Asset underneath Material inside the Set Material node. In the asset finder window
that appears, type red in the search box to find the TargetRed material we created
earlier. Clicking on this asset will attach it to the Material field inside the Set
Material node.
We have now done everything we need with this Blueprint to turn the target cylinder
red, but before the Blueprint can be saved, it must be compiled. Compiling is the
process used to convert the developer-friendly Blueprint language into machine
instructions that tell the computer what operations to perform. This is a handsoff process, so we don't need to concern ourselves with it, except to ensure that
we always compile our Blueprint scripts after we assemble them. To do so, hit the
Compile button in the top-left corner of the editor toolbar, and then click on Save.
[ 15 ]
Now that we have set up a basic gameplay interaction, it is wise to test the game to
ensure that everything is happening the way we expect. Click on the Play button,
and a game window will appear directly above the Blueprint Editor. Try both
shooting and running into the CylinderTarget actor you created.
Chapter 1
You will learn more about these pin types as we get into more complicated
Blueprints; for now, we only need to concern ourselves with the blue output pin
labeled Other, which contains the data about which other actor performed the
hitting to fire this event. This will be useful in order for us to ensure that the cylinder
target changes color only when hit by a projectile fired from the player, rather than
changing color because of any other actors that might bump into it.
To ensure that we are only triggering in response to a player projectile hit, click
and drag a wire from the Other output pin to empty space. In this search window,
type projectile. You should see some results that look similar to the following
screenshot. The node we are looking for is called Cast To FirstPersonProjectile:
template that controls the behavior of the projectiles that are fired from your gun.
This node uses casting to ensure that the action attached to the execution pin of this
node occurs only if the actor hitting the cylinder target matches the object referenced
by the casting node.
When the node appears, you should already see a blue wire between the Other
output pin of the Event Hit node and the Object pin of the casting node. If not, you
can generate it manually by clicking and dragging from one pin to the other. You
should also remove the connections between the Event Hit and Set Material node
execution pins so that the casting node can be linked between them. Removing a wire
between two pins can be done by holding down the Alt key and clicking on a pin.
[ 17 ]
Once you have linked the three nodes, the event graph should look like what is
shown in the following screenshot:
Now compile, save, and click on the play button again to test. This time, you should
notice that the cylinder target retains its default color when you walk up and touch
it, but does turn red when fired upon by your gun.
Adding movement
Now that we have a target that responds to the player shooting, we can add some
sort of challenge to start making our project feel like a game. A simple way to do
this is to add some movement to our target. To accomplish this, we will first have to
declare that our target actor is an object that is intended to move, and then we need
to set up logic within the Blueprint that will manage how it moves. Our goal will be
to make the target cylinder move back and forth across our level.
[ 18 ]
Chapter 1
By default, basic actors that are placed in the world are set to static.
"Static" means that the object cannot move or be manipulated during
gameplay. Static objects are significantly less resource intensive to
render, and this should be our default choice for non-interactive
objects so that we can maximize frame rates.
It is important to note that the version of the target cylinder that we changed in the
level is just one instance of the Blueprint template for the target cylinders that we
have created. An instance refers to an actual object that has been created, whereas
our Blueprints are descriptions of the kind of features that those instances will have
once they are created.
Any changes we make to a target cylinder already inside the level will be made
for that particular target cylinder only. To make changes to all future targets, we
need to modify the Blueprint directly. To do so, open CylinderTarget_Blueprint
again, either by navigating to the open tab in the editor, or by double-clicking on
the CylinderTarget_Blueprint file in your Blueprints folder.
With the Blueprint open, we want to navigate to the Viewport tab located
underneath the menu toolbar. Along the left side, you will see the Components
panel, which lists all the components that make up this Blueprint. Since we
want to edit a property of the physical object, or mesh, we click on component
StaticMeshComponent. You will see a familiar-looking details panel. It includes the
same properties and categories that we saw when we edited the target cylinder in
the level editing interface. Here, we have to switch the same Mobility toggle, located
beneath the Transform properties, from Static to Movable. This will ensure that all
future targets created from this Blueprint will already be set to be moveable.
[ 19 ]
Because we want to target this object with our gun, we also need to ensure that the
target is capable of being collided with so that our bullets don't pass through it. Still
in the details panel, find the category called Collision and look for Collision Presets
in the drop-down menu. There are many other options in this dropdown, and by
choosing the Custom option, you can even set the object's collision interaction with
different object types individually. For our purpose, we just need to ensure that
this drop-down menu is set to BlockAllDynamic, which ensures that the mesh
will register collisions with any other object that also has a collider.
To understand where the object currently is, we need to get some information about
the world itself. Specifically, what are the coordinates of the cylinder in the world?
The speed and direction are the values we are going to provide to the Blueprint,
though some calculations will be necessary to turn those values into information
that is useful for the Blueprint to move the object.
[ 20 ]
Chapter 1
[ 21 ]
Chapter 1
To do this, drag the Speed variable onto the event graph and choose Get to create
the speed node. Now, right-click on empty graph space and search for delta. Select
the Get World Delta Seconds option to place the corresponding node. Finally, drag
the output pin from either the delta seconds node or the speed node, and drop it into
empty space. Type an asterisk in the search field (Shift + 8 on most computers) and
select the Float * Float node. Finally, drag the other output pin onto the remaining
input pin of the new multiplication node to multiply these two values, like this:
[ 23 ]
Now we need to add the necessary nodes to add speed and direction to the location
information we just extracted. Right-click on empty grid space and search and select
the Make Transform node. This will mark the end of your calculations, so make
sure that it is positioned to the right of all of your other nodes. The Make Transform
node has three inputs, Location, Rotation, and Scale. The Rotation and Scale inputs
should be connected to the rotation and scale output pins on the Break Transform
node we created earlier.
Next, we need to multiply the Direction vector and the Speed float we calculated.
Drag the output node of the Normalize node into empty space, and search using an
asterisk. Select Vector * Float and connect the green input pin to the output of the
float multiplication node that we used with speed.
Our final calculation step is to add Speed and Direction to the current location
we calculated. Click on the yellow vector output pin of the new multiplication
node, and drag it onto empty space. Search using + and select the Vector + Vector
node. Ensure that one input pin of this addition node is connected to the previously
mentioned vector multiplication node, and then connect the other input pin is
connected to the Location output pin of the Break Transform node. Finally, drag the
output pin of our addition node onto the Location input pin of the Make Transform
node. When you are finished, the result should look like what is shown in the
following screenshot:
[ 24 ]
Chapter 1
Updating location
Now that we have the transform calculated, we can adjust the location of our target
actor by this value. We used delta time to make our speed and direction changes
consistent across frames, and as a consequence, we can simply use the Event Tick node
to fire our move action every frame. Right-click on empty grid space, search for Event
Tick, and place the node somewhere to the right of your Make Transform node.
To move the actor, we will be using the Set Actor Transform node. Drag a wire
from the execution pin of Event Tick to empty grid space, and search for Set Actor
Transform. Place the node, and then connect the Return Value output pin on your
Make Transform node to the New Transform input pin on the Set Actor Transform
node, as shown here:
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.
[ 25 ]
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.
[ 26 ]
Chapter 1
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.
Summary
You have built your first prototype using Unreal 4 Blueprints. Congratulations! The
amount of progress you have already made is commendable.
In this chapter, you created a project and an initial level using a first-person shooter
template. You then set up a target object that reacts to the player's gunfire by
changing appearance. Finally, you set up a Blueprint that will allow you to rapidly
create moving targets. The skills you have learned here will serve as a strong
foundation for building more complex interactive behavior in later chapters,
or even entire games of your own making.
You may wish to spend some additional time modifying your prototype to include
a more appealing layout, or feature faster moving targets. As we continue building
our game experience, remember that you always have the opportunity to linger on
a section and experiment with your own functionality or customizations. One of the
greatest benefits of Blueprint's visual scripting is the speed at which you can test
new ideas, and each additional skill that you learn will unlock exponentially more
possibilities for game experiences that you can explore and prototype.
In the next chapter, we will be looking more closely at the player controller that
came with the First Person template. We will extend the existing Blueprint that
governs player movement and shooting with a gun that is tweaked to our liking,
and produces a more interesting visual impact and sound effects.
[ 27 ]
www.PacktPub.com
Stay Connected: