Unreal Engine 4 AI Programming Essentials - Sample Chapter
Unreal Engine 4 AI Programming Essentials - Sample Chapter
"Community
Experience
Distilled"
Jie Feng
$ 34.99 US
22.99 UK
C o m m u n i t y
Peter L. Newton
Unreal Engine 4 AI
Programming Essentials
E x p e r i e n c e
Unreal Engine 4 AI
Programming Essentials
Create responsive and intelligent game AI using Blueprints
in Unreal Engine 4
D i s t i l l e d
Peter L. Newton
Jie Feng
technology grew, web applications were his first exploration into development. The
excitement of programming is what kept Peter diving further into different software
designs and programming patterns. He is a self-taught programmer who has spent
countless hours in reverse engineering assembly and arm instruction executables
just for the joy of learning. Peter has several years of experience as a web developer,
software developer, database architect, and hardware technician. His recent years
were dedicated to the Virtual Reality/Gaming industry experience, working with
such companies as Create, Sony Pictures, and the developers of Unreal Engine 4,
Epic Games.
Peter's most recent VR project was Can You Walk The Walk?, which won
Digital Hollywood's "Best In Virtual Reality Based on a Cinematic or Television
Experience" award.
Preface
Artificial Intelligence (AI) is an essential part of any game. It makes the virtual
world we create more immersive and fun to play in. Game AI is different from the
general scientific AI that we know; it is more targeted to solve key problems in game
design, including navigation, which is how a nonplayer character (NPC) should
move from one point to another and avoid obstacles; decision making, which is how
to perform certain actions based on different situations; and environment sensing,
which is the ability to understand what exists in the environment and what its status
is. These techniques make it possible to create a dynamic and realistic gameplay so
that the players will be more engaged in the world that is created for them.
Game AI is complicated and brings a lot of challenges if you want to develop on your
own. Unreal Engine 4 is a powerful game engine that provides rich functionalities to
create cross-platform 3D and 2D games. It is well known for its advanced graphics
and highly customizable components. Now, it is free to use and open source, which
makes it one of the most popular game engines out there. Unreal Engine 4 comes
with a complete suite of tools for game AI, including NavMesh, Behavior Trees, and
Environment Query System. With these tools in hand, it is much easier to bring AI
to your games. For game designers, you can even use a visual scripting tool called
Blueprints to build your game logic, including AI, by just connecting nodes and
without even writing a single line of code.
This book is our effort to introduce these wonderful tools in Unreal Engine 4 to build
game AI to game creators who are interested in making their virtual world more
interesting. It will cover all the components we have mentioned and show you how
to use each tool to build different character behaviors and combine them to create
more complex scenes.
We can't wait to see what you will create!
Preface
Introduction to Game AI
This chapter will introduce the basic idea of Artificial Intelligence (AI) and how it
directly affects and enhances the gaming experience. You will learn the differences
between the traditional and also the game-specific goals of AI. We will introduce
various techniques used in game AI, including navigation, Behavior Tree, sensor
systems, and so on. You will learn in brief which tools we utilize for AI within Unreal
Engine 4's editor. After this chapter, readers will gain a basic understanding of how
AI can be applied to game development for a better gaming experience. The AI
techniques that we will briefly cover here will be taught in the subsequent chapters.
[1]
Introduction to Game AI
Navigation
Navigation for AI is usually built up of the following tools:
[2]
Chapter 1
Path Following (Path nodes): A similar solution to NavMesh, Path nodes can
designate the space in which the AI traverses:
Behavior Tree: Using Behavior Tree to influence your AI's next destination
can create a more varied player experience. It not only calculates its requested
destination, but also decides whether it should enter the screen with a cart
wheeling double-back flip, no hands, or the triple somersault and jazz hands.
Sensory systems: Sensory systems can provide critical details, such as the
nearby players, sound levels, nearby cover, and many other variables of the
environment that can alter movement. It's critical that your AI understands
the changing environment so that it doesn't break the illusion of being
a real opponent.
[3]
Introduction to Game AI
While all these components aren't necessary to achieve AI navigation, they all
provide critical feedback, which can affect the navigation. Navigating within
a world is limited only by pathways within the game. We can see an example
of group behavior with several members following a leader here:
[4]
Chapter 1
[5]
Introduction to Game AI
Now, we will discuss the components found within UE4 Behavior Tree.
Root
This node is the beginning node that sends the signal to the next node in the tree. This
connects to a composite, which begins your first tree. What you may notice is that you
are required to use a composite first to define a tree and then to create a task for this
tree. This is because hierarchical FSM creates branches of states. These states will be
populated with other states or tasks. This allows an easy transition among multiple
states. You can see what a root node looks like as shown in the following screenshot:
[6]
Chapter 1
Decorators
Decorators are conditional statements (the blue part on top of a node) that control
whether or not a branch in the tree or even a single node can be executed. I used a
decorator in the AI we will make to tell it to update to the next available route.
In the following image, you can note the Attack & Destroy decorator that defines the
state on top of the composite. This state includes two tasks, Attack Enemy and Move
To Enemy, which also has a decorator telling it to execute only when the bot state is
Search:
In the preceding screenshot, you can note the Attack & Destroy decorator that
defines the state on top of the composite. This state includes two tasks, Attack
Enemy and Move To Enemy, which also has a decorator telling it to execute only
when the bot state is Search.
Composites
These are the beginning points of the states. They define how the state will behave
with returns and execution flow. They have three main types: Selector, Sequence,
and Simple Parallel. This beginning branch has a conditional statement, if the state
is equal or greater than Search state:
[7]
Introduction to Game AI
Selector executes each of its children from left to right and doesn't fail; however,
it returns success when one of its children returns success. So, this is good for
a state that doesn't check for successfully executed nodes. The following screenshot
shows an example of Selector:
Sequence executes its children in a similar fashion to Selector but returns fail when one
of its children returns fail. This means that it's required that all nodes return success to
complete the sequence. You can see a Sequence node in the following screenshot:
Last but not least, Simple Parallel allows you to execute a task and a tree essentially
at the same time. This is great for creating a state that requires another task to always
be called. To set it up, you need to first connect it to a task that it will execute. The
second task or state connected continues to be called with the first task until the first
task returns success.
[8]
Chapter 1
Services
Services run as long as the composite it is added to stays activated. They tick at
the intervals you set within the properties. They have another float property called
Tick Interval that allows you to control how often this service is executed in the
background. Services are used to modify the state of AI in most cases because it's
always called. For example, in the bot that we will create, we will add a service
to the first branch of the tree so that it's called without interruption and will be able
to maintain the state that the bot should be in at any given movement. The green
node in the following screenshot is a service with important information explicitly:
This service, called Detect Enemy, actually runs a deviating cycle that updates
Blackboard variables such as State and Enemy Actor.
Tasks
Tasks do the dirty work and report success or failed if it's necessary. They have
blueprint nodes that can be referred to in Behavior Tree. There are two types of
nodes that you'll use most often when working with Task: Event Receive Execute,
which receives the signal to execute the connected scripts, and Finish Execute, which
sends the signal back and returns true or false on success. This is important when
making a task meant for the Sequence composite node.
[9]
Introduction to Game AI
Blackboard
A Blackboard is an asset to store the variables to be used within the AI Behavior Tree.
They are created outside Behavior Tree. In our example, we will store an enumeration
variable for the state in the State, EnemyActor object to hold the currently targeted
enemy, and Route to store the current route position that the AI is requested to travel
to, just to name a few. You can see all current variables as keys in Blackboard panel
as follows:
They work just by setting a public variable of a node to one of the available
Blackboard variables in the drop-down menu. The naming convention in the
following screenshot makes this process streamlined:
[ 10 ]
Chapter 1
Sensory systems
A sensory system usually consists of several modules, such as sight, sound, and
memory, to help the AI capture information about the environment. A bot can
maintain the illusion of intelligence using sounds within their environment to make
a deliberate risk assessment before engaging a hazardous threat or aiding a nearby
teammate who is calling for help. The use of memory will allow the bot to avoid an
area where it remembers seeing a severe threat or rush back to an area where it last
saw its group. Creating a sensory system in the case of an enemy player is heavily
based on the environment where the AI fights the player. It needs to be able to find
cover, evade the enemy, get ammo, and other features that you feel create immersive
AI for your game. A game with AI that challenges the player creates a unique
individual experience. A good sensory system contributes critical information that
makes for reactive AI. In this project, we will use the sensory system to detect the
pawns that the AI can see. We will also use functions to check for the line of sight of
the enemy. We will check whether there is another pawn in the way of our path. We
can check for cover and other resources within the area.
Machine learning
Machine learning is a branch on its own. This technique allows AI to learn from
situations and simulations. Inputs are taken from the environment, including the
context in which the bot allows it to make decisive actions. In machine learning, the
inputs are put within a classifier that can predict a set of outputs with a certain level
of certainty. Classifiers can be combined into ensembles to increase the accuracy of
probabilistic prediction. We won't dig deep into this subject, but there exist a vast
amount of resources for studying machine learning, ranging from text books (Pattern
Recognition and Machine Learning by Christopher M. Bishop, Springer) to online courses
(Machine Learning on coursera.org).
Tracing
Tracing allows another actor within the world to detect objects by ray tracing. A
single line trace is sent out, and if it collides with an actor, the actor is returned along
with information on the impact. Tracing is used for many reasons; one way it is used
in FPS is to detect hits. Are you familiar with the hit box? When your player shoots in
a game, a trace is shot out that collides with the opponent's hit box, determining the
damage to the player, and if you're skillful enough, it results in death. Other shapes
available for traces, such as spheres, capsules, and boxes, allow tracing for different
situations. Recently, I used Box Trace for my car to detect objects near it.
[ 11 ]
Introduction to Game AI
Influence Mapping
Influence Mapping isn't a finite approach; it's the idea that specific locations on the
map would be attributed information that directly influences the player or AI. An
example of using Influence Mapping with AI is presence falloff. Let's say we have
other enemy AI in a group; their presence map would create a radial circle around
the group with the intensity based on the size of the group. This way, the other AI
knows by entering this area that they're entering a zone occupied by other enemy AI.
Practical information isn't the only thing people use it for, so just understand that
it's meant to provide another level of input to help your bot make more additional
decisions. As shown in the following image, different colors represent zones
occupied by different types of AI, and color intensity indicates the influence
with respect to each AI character:
Practical information isn't the only thing people use it for, so just understand
that it's meant to provide another level of input to help your bot make more
additional decisions.
[ 12 ]
Chapter 1
Behavior Tree: This is used to create different states and the logic behind AI.
Blackboard Asset: These are used to store information. They act as the local
variable for AI.
Enumeration: This is used to create states, which you can alternate between.
Target Point: Our Waypoints class is derived from the Target Point class,
which we will use to create a basic form of Path node.
There are two types of NavMesh volume. The first, the NavMesh Bounds volume,
defines the area for NavMesh. The Nav Modifier volume, when supplied with a Nav
Area class, affects the NavMesh Bounds volume's navigation attributes where the
two intersect.
[ 13 ]
Introduction to Game AI
Summary
In this chapter, we started by introducing game AI and discussing why it is
important for our gaming experience. Then, we illustrated most of the used game
AI techniques and what they are capable of. The corresponding UE4 tools for game
AI were also mentioned to provide a bigger picture of the content we will cover
throughout this book. In the next chapter, we will create our basic AI by setting
up an AI-controlled player and adding some simple behavior to it.
[ 14 ]
www.PacktPub.com
Stay Connected: