behavior tree in unreal engine 4

72
Behavior Tree in Unreal Engine 4 [email protected] 1

Upload: huey-park

Post on 19-Jul-2015

616 views

Category:

Software


10 download

TRANSCRIPT

Page 1: Behavior Tree in Unreal engine 4

Behavior Treein Unreal Engine [email protected]

1

Page 2: Behavior Tree in Unreal engine 4

Contents

● What and Why?● Theory● Practice● Q & A● Reference

2

Page 3: Behavior Tree in Unreal engine 4

What?

3

Page 4: Behavior Tree in Unreal engine 4

What?

● Commonly used way to direct behaviors for AI in a game.

● They can be used for any decision-making(in systems that aren’t just AI)

4

Page 5: Behavior Tree in Unreal engine 4

Why?

5

Page 6: Behavior Tree in Unreal engine 4

Why?

● Offer a good balance of supporting goal-oriented(like a*) behaviors and reactivity(like flocking).

6

Page 7: Behavior Tree in Unreal engine 4

Why?

● Offer a good balance of supporting goal-oriented(like a*) behaviors and reactivity(like flocking).

7

Page 8: Behavior Tree in Unreal engine 4

Why?

● Offer a good balance of supporting goal-oriented(like a*) behaviors and reactivity(like flocking).

8

Page 9: Behavior Tree in Unreal engine 4

Why not other solutions?

9

Page 10: Behavior Tree in Unreal engine 4

Why not use other solutions?

● There are innumerable ways to implement AI or other decision making systems.

● Not necessarily always best, but they are frequently great for games. (LOL, Uncharted 2, ...)

10

Page 11: Behavior Tree in Unreal engine 4

Why not use other solutions?

State Machine?● While state machines are reasonably

intuitive for simple cases, as they become more complex they are hard to keep goal-oriented. As the number of states increases, the transitions between states become exponentially complex. Hierarchical state machines help a little, but many of the same issues remain.

11

Page 12: Behavior Tree in Unreal engine 4

10 Reasonsthe Age of

Finite State Machinesis Over

12

Page 13: Behavior Tree in Unreal engine 4

#1 They’re Unorthodox

Problem : Building a FSM is a very different process from any other form of software engineering. Sure, the concept is “designer friendly” but a surprisingly small amount of mainstream programming knowledge applies to FSMs.

13

Page 14: Behavior Tree in Unreal engine 4

#1 They’re Unorthodox

Reason : FSMs require each state to be wired with an explicit transition to the next state. No programming language requires this; everything is done implicitly on the semantics of the language itself.

14

Page 15: Behavior Tree in Unreal engine 4

#2 They’re Low-Level

Problem : The process of editing the logic of a FSM is very low-level and quite mechanical. You often find rebuilding the similar behaviors over and over from scratch - which takes a lot of time.

15

Page 16: Behavior Tree in Unreal engine 4

#2 They’re Low-Level

Reason : All you can do is edit transitions from a state to another. There’s no way to capture higher-level patterns that reoccur frequently like sequences or conditionals. Meta-programming doesn’t exist in the world of finite state machines.

16

Page 17: Behavior Tree in Unreal engine 4

#3 Their Logic is Limited

Problem : Finite state machines, as they are defined formally, are computationally limited. This means you can’t do things like counting by default.

17

Page 18: Behavior Tree in Unreal engine 4

#3 Their Logic is Limited

Reason : If you consider events as symbols, you can only recognize regular grammars with a finite state automaton. Likewise, finite state machines can only act as transducers for regular language.

18

Page 19: Behavior Tree in Unreal engine 4

#4 They Require Custom Extensions

Problem : Game developers often use extensions to make FSMs useful in practice. However, these hacks aren’t always easy to understand and aren’t so well documented either — unlike the academic foundations of FSMs.

19

Page 20: Behavior Tree in Unreal engine 4

#4 They Require Custom Extensions

Reason : Because FSMs are theoretically limited, developers must add functionality externally to implement certain features. This means leveraging the underlying programming language to implement things like counters, timers, or any form of memory. 20

Page 21: Behavior Tree in Unreal engine 4

#5 They Are Hard to Standardize

Problem : Unlike planners (HTN) or search algorithms (A*) which are implemented in relatively common ways, FSMs are very difficult to reuse across multiple games or in different parts of the engine.

21

Page 22: Behavior Tree in Unreal engine 4

#5 They Are Hard to Standardize

Reason : Since FSMs aren’t turing complete, FSMs need customizing to be able to deal with the special cases of each problem. This makes them very narrowly applicable, unlike scripting languages which can easily be repackaged.

22

Page 23: Behavior Tree in Unreal engine 4

#6 They Are Not Deliberative

Problem : It takes a lot of work to use a FSMs to create goal-directed behaviors. This is an issue as most purposeful AI will require dealing with long-term goals.

23

Page 24: Behavior Tree in Unreal engine 4

#6 They Are Not Deliberative

Reason : FSMs operate in a reactive mode, only dealing with events and triggering transitions. They are not capable of searching ahead, so you have to edit the transitions for dealing with all the different goals manually.

24

Page 25: Behavior Tree in Unreal engine 4

#7 They Have Concurrency Nightmares

Problem : FSMs just don’t like concurrency. When running multiple state machines in parallel, you either end up with deadlocks or you have edit them all in a way they are compatible.

25

Page 26: Behavior Tree in Unreal engine 4

#7 They Have Concurrency Nightmares

Reason : FSMs have as much trouble dealing with external resource conflicts as they do storing information. Solutions to make state machines concurrent are typically external to the FSM itself.

26

Page 27: Behavior Tree in Unreal engine 4

#8 They Scale Poorly

Problem : Finite state machines, even hierarchical ones, don’t scale very well. They often end up being edited as a large block of logic, instead of behaviors edited modularly.

27

Page 28: Behavior Tree in Unreal engine 4

#8 They Scale Poorly

Reason : FSMs are not built with the many mechanisms of programming languages that help them scale up to solve large problems, in particular indirection. Also, FSMs provide no easy way to synchronize multiple modular behaviors together.

28

Page 29: Behavior Tree in Unreal engine 4

#9 They Are Labor Intensive

Problem : It takes a lot of work to wire up a FSM to implement any design. Certain problems occur only because of the state machine itself!

29

Page 30: Behavior Tree in Unreal engine 4

#9 They Are Labor Intensive

Reason : Dealing with all the challenges mentioned previously takes a lot of time by the designers, and this ultimately becomes a source of bugs in the behaviors.

30

Page 31: Behavior Tree in Unreal engine 4

#10 Industry is Moving On

Fact : Experienced game developers are using finite state machines less and less, switching to alternatives like behavior trees.

31

Page 32: Behavior Tree in Unreal engine 4

#10 Industry is Moving On

Fact : Middleware vendors for game AI are focusing their development efforts mainly on planners, and 2008 should see more of these becoming available off the shelf.

32

Page 33: Behavior Tree in Unreal engine 4

Theory

33

Page 34: Behavior Tree in Unreal engine 4

Theory

34

Sense

ThinkAct

Page 35: Behavior Tree in Unreal engine 4

Theory

● Generally rely on physics engine

● Usually very expensive

● Use infrequently

35

Sense

ThinkAct

Page 36: Behavior Tree in Unreal engine 4

Theory

● Decision Logic

● Generally quite simple

● Design intensive

36

Sense

ThinkAct

Page 37: Behavior Tree in Unreal engine 4

Theory

● Action execution

● Often long running

● Can fail to complete

37

Sense

ThinkAct

Page 38: Behavior Tree in Unreal engine 4

BehaviorTree

Theory

38

Sense

ThinkAct

Memory

Page 39: Behavior Tree in Unreal engine 4

Unreal Engine 4Behavior Tree

39

Page 40: Behavior Tree in Unreal engine 4

BehaviorTree

Unreal Engine 4 Behavior Tree

40

Service

DecoratorTask

Blackboard

Page 41: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

41

Page 42: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Root

Composite

Service

Decorator

Task

42

Page 43: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Root

● The starting execution node for the Behavior Tree.

● Every Behavior Tree has one.● You cannot attach Decorators or

Services to it.

43

Page 44: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Composite

● These are nodes that define the root of a branch and define the base rules for how that branch is executed.

● Sequence, Selector, Simple Parallel

44

Page 45: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Composite : Sequence

45

Page 46: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Composite : Sequence

● Sequence Node execute their children from left to right, and will stop executing its children when one of their children Fails. If a child fails, then the Sequence fails.

46

Page 47: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Composite : Selector

47

Page 48: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Composite : Selector

● Selector Nodes execute their children from left to right, and will stop executing its children when one of their children Succeeds. If a Selector’s child succeed, the Selector succeeds.

48

Page 49: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Composite : Simple Parallel

49

Page 50: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Composite : Simple Parallel

● The Simple Parallel node allows a single main task node to be executed along side of a full tree. When the main task finishes, the setting in Finish Mode dictates the secondary tree.

50

Page 51: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Service

51

Page 52: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Service

● These attach to Composite nodes, and will execute at their defined frequency. These are often used to make checks and to update the Blackboard. These take the place of traditional Parallel nodes.

52

Page 53: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Decorator

53

Page 54: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Decorator

● Also known as conditionals. These attach to another node and make decisions on whether or not a branch in the tree, or even single node, can be executed.

54

Page 55: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Task

55

Page 56: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Task

● Theres are leaves of the tree, the nodes that “do” things.

56

Page 57: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Blackboard

57

Page 58: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Blackboard

● A blackboard is a simple place where data can be written and read for decision making purposes.

● A blackboard can be used by a single AI pawn, shared by squad.

58

Page 59: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Blackboard : Why use?

● To make efficient event-driven behaviors

● To cache calculations● As a scratch-pad for behaviors● To centralize data

59

Page 60: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Blackboard : Notice

● Don’t clutter the blackboard with lots of super-specific-case data.

● If only one node needs to know something, it can potentially fetch the value itself rather than adding every bit of tree.

60

Page 61: Behavior Tree in Unreal engine 4

Unreal Engine 4 Behavior Tree

Blackboard : Notice

● If you fail to copy data to the blackboard properly, it may cause you some debugging nightmare!

● If very frequently updated and copied to the blackboard, that could be bad for performance.

61

Page 62: Behavior Tree in Unreal engine 4

Practice

62

Page 64: Behavior Tree in Unreal engine 4

Airplane Dogfighting

State

● When I track● When I get tracking● When I can’t find target● When I suddenly encountered an

obstacle

64

Page 65: Behavior Tree in Unreal engine 4

Airplane Dogfighting

Task

● Move to target● Evasion maneuver● Cobra maneuver● Emergency evasion● Throttle control

65

Page 66: Behavior Tree in Unreal engine 4

Airplane Dogfighting

Service

● Search airplane● Emergency evasion check

66

Page 67: Behavior Tree in Unreal engine 4

Airplane Dogfighting

Decorator

● Blackboard Based Condition● Time Limit● Emergency Evasion Check● In View

67

Page 68: Behavior Tree in Unreal engine 4

68

Page 69: Behavior Tree in Unreal engine 4

69

Page 70: Behavior Tree in Unreal engine 4

Airplane Dogfighting

https://github.com/HueyPark/Open-Fire

70

Page 71: Behavior Tree in Unreal engine 4

Q & A

71