Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Unity 2017 Game AI Programming - Third Edition - Third Edition

You're reading from  Unity 2017 Game AI Programming - Third Edition - Third Edition

Product type Book
Published in Jan 2018
Publisher Packt
ISBN-13 9781788477901
Pages 254 pages
Edition 3rd Edition
Languages

Table of Contents (10) Chapters

Preface 1. The Basics of AI in Games 2. Finite State Machines and You 3. Implementing Sensors 4. Finding Your Way 5. Flocks and Crowds 6. Behavior Trees 7. Using Fuzzy Logic to Make Your AI Seem Alive 8. How It All Comes Together 9. Other Books You May Enjoy

Behavior Trees

Behavior trees (BTs) have been gaining popularity among game developers very steadily. Over the last decade, BTs have become the pattern of choice for many developers when it comes to implementing behavioral rules for their AI agents. Games such as Halo and Gears of War are among the more famous franchises to make extensive use of BTs. An abundance of computing power in PCs, gaming consoles, and mobile devices has made them a good option for implementing AI in games of all types and scopes.

In this chapter, we will cover the following topics:

  • The basics of a behavior tree
  • The benefits of using existing behavior tree solutions
  • How to implement our own behavior tree framework
  • How to implement a basic tree using our framework

Technical Requirements

Learning the basics of behavior trees

Behavior trees got their name from their hierarchical, branching system of nodes with a common parent, known as the root. As you've surely learned by now from reading this book, behavior trees too mimic the real thing they are named after—in this case, trees and their branching structure. If we were to visualize a behavior tree, it would look something like the following figure:

A basic tree structure

Of course, behavior trees can be made up of any number of nodes and child nodes. The nodes at the very end of the hierarchy are referred to as leaf nodes, just like a tree. Nodes can represent behaviors or tests. Unlike state machines, which rely on transition rules to traverse through them, a BT's flow is defined strictly by each node's order within the larger hierarchy. A BT begins evaluating from the top of the tree...

Evaluating the existing solutions

The Unity asset store is an excellent resource for developers. Not only are you able to purchase art, audio, and other kinds of assets, but it is also populated with a large number of plugins and frameworks. Most relevant to our purposes, there are a number of behavior tree plugins available on the asset store, ranging from free to a few hundred dollars. Most, if not all, provide some sort of GUI to make visualizing and arranging a fairly painless experience.

There are many advantages of going with an off-the-shelf solution from the asset store. Many of the frameworks include advanced functionality such as runtime (and often visual) debugging, robust APIs, serialization, and data-oriented tree support. Many even include sample leaf logic nodes to use in your game, minimizing the amount of coding you have to do to get up and running.

The previous...

Implementing a basic behavior tree framework

While a fully-fledged implementation of a behavior tree with a GUI and its many node types and variations is outside the scope of this book, we can certainly focus on the core principles to get a solid grasp of what the concepts we've covered in this chapter look like in action. Provided with this chapter is the basic framework for a behavior tree. Our example will focus on simple logic to highlight the functionality of the tree, rather than muddy up the example with complex game logic. The goal of our example is to make you feel comfortable with what can seem like an intimidating concept in game AI, and give you the necessary tools to build your own tree and expand upon the provided code if you do so.

Implementing a base Node class...

Testing our framework

The framework that we just reviewed is nothing more than this. It provides us with all the functionality we need to make a tree, but we have to make the actual tree ourselves. For the purposes of this book, a somewhat manually constructed tree is provided.

Planning ahead

Before we set up our tree, let's look at what we're trying to accomplish. It is often helpful to visualize a tree before implementing it. Our tree will count up from zero to a specified value. Along the way, it will check whether certain conditions are met for that value and report its state accordingly. The following diagram illustrates the basic hierarchy for our tree:

For our tests, we will use a three-tier tree, including...

HomeRock card game example

To further illustrate potential uses for BTs, let's take a look at the second example for this chapter included in the sample code. In the CardGame Unity scene, you'll find an implementation of a turn-based card game, where the player and the AI opponent have three different abilities: attack, heal, and defend. The user gets to pick which ability to use on their turn, and the AI will use a BT to decide which course of action to take. The game ends when a player reaches 0 hit points. The following image illustrates our game view:


Game screen for HomeRock—Heroes of Unity

As you can see here, the player can select their attack by clicking on one of the cards, which have been spruced up with some flavor text. The player's hit points are displayed in the lower-left corner, and the AI enemy's hit points are displayed in the top...

Summary

In this chapter, we dug into how a behavior tree works, and then we looked at each individual type of node that can make up a behavior tree. We also learned the different scenarios where some nodes would be more helpful than others. After looking at some off-the-shelf solutions available in the Unity asset store, we applied this knowledge by implementing our own basic behavior tree framework in C# and explored its inner workings. With the knowledge and the tools out of the way, we created a sample behavior tree using our framework to test the concepts learned throughout the chapter. We then went on to explore the implementation of HomeRock, a sample card game that showcases an AI opponent. This knowledge prepares us to harness the power of behavior trees in games and take our AI implementations to the next level.

In the next chapter, Chapter 7, Using Fuzzy Logic to Make...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Unity 2017 Game AI Programming - Third Edition - Third Edition
Published in: Jan 2018 Publisher: Packt ISBN-13: 9781788477901
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}