﻿1
00:00:04,310 --> 00:00:05,240
‫Hello and welcome.

2
00:00:05,240 --> 00:00:11,660
‫In this lecture, we're introducing two new types of asset the behavior tree and the blackboard, which

3
00:00:11,660 --> 00:00:16,640
‫are two assets that allow us to do very interesting things with A.I..

4
00:00:16,640 --> 00:00:18,740
‫Allow us to make much more complicated.

5
00:00:18,740 --> 00:00:23,600
‫I very easily let's dive in and see how we use them and set them up.

6
00:00:25,140 --> 00:00:25,410
‫Okay.

7
00:00:25,410 --> 00:00:28,970
‫So we want to create some sophisticated AI behavior.

8
00:00:28,980 --> 00:00:33,960
‫Now, the way we typically do this in Unreal is using a system called behavior trees.

9
00:00:33,990 --> 00:00:35,050
‫Now, what are these?

10
00:00:35,070 --> 00:00:38,990
‫Well, the best way for us to find out is to create one.

11
00:00:39,000 --> 00:00:41,970
‫So let's go ahead and right click and go.

12
00:00:41,970 --> 00:00:45,150
‫We're looking here under the artificial intelligence.

13
00:00:45,150 --> 00:00:48,450
‫And there's two options here behavior, tree and blackboard.

14
00:00:48,480 --> 00:00:51,050
‫We are going to want both of these.

15
00:00:51,060 --> 00:00:54,650
‫So let's go and start with the behavior tree.

16
00:00:54,660 --> 00:00:59,670
‫The prefix for this is BHT and we're going to call this the enemy.

17
00:00:59,670 --> 00:01:01,320
‫So behavior tree enemy.

18
00:01:01,350 --> 00:01:03,780
‫I just finish off with that.

19
00:01:03,780 --> 00:01:04,490
‫There you go.

20
00:01:04,500 --> 00:01:09,180
‫And we also need to create the other thing in there, which was a blackboard.

21
00:01:09,180 --> 00:01:11,250
‫Now, what on earth is a blackboard?

22
00:01:11,250 --> 00:01:12,300
‫We'll get to that in just a second.

23
00:01:12,300 --> 00:01:15,930
‫But we're just going to call it the b b underscore enemy.

24
00:01:15,940 --> 00:01:19,110
‫I said before, blackboard beat for behavior tree.

25
00:01:19,110 --> 00:01:22,320
‫And while we're at it, we've got a few things relating to I here.

26
00:01:22,320 --> 00:01:29,100
‫I'm going to create a new folder called A.I. and we are going to move the blackboard, the behavior

27
00:01:29,100 --> 00:01:29,640
‫tree.

28
00:01:29,700 --> 00:01:33,900
‫And somewhere here we have got our shooter, AI controller.

29
00:01:33,900 --> 00:01:40,530
‫All of that goes into I move here and then we can go into our AI folder to deal with everything AI related.

30
00:01:41,070 --> 00:01:45,450
‫Let's open up our behavior tree first of all and dock it to the main window.

31
00:01:45,660 --> 00:01:47,550
‫So what on earth is this?

32
00:01:47,550 --> 00:01:52,890
‫So this allows us to create trees of behaviors where you might say we've got part of the behavior here

33
00:01:52,890 --> 00:01:53,880
‫is for tracing.

34
00:01:53,880 --> 00:01:55,350
‫Part of it is for trolling.

35
00:01:55,350 --> 00:02:00,180
‫Part of it is for something else, opening doors, all these kinds of things.

36
00:02:00,180 --> 00:02:06,480
‫They typically structure behaviors as lots and lots of trees of behaviors and sub trees and so on.

37
00:02:06,480 --> 00:02:14,070
‫We will see that all going on in detail, but suffice to say it is a great way of putting together natural

38
00:02:14,070 --> 00:02:15,780
‫behaviors in your games.

39
00:02:16,080 --> 00:02:17,610
‫Now it's not the be all and end all.

40
00:02:17,610 --> 00:02:19,440
‫There's lots of AI techniques.

41
00:02:19,440 --> 00:02:23,820
‫This might not be the one that you want to go with for your game, but it's a great start and you can

42
00:02:23,820 --> 00:02:26,040
‫achieve quite a lot with it.

43
00:02:26,520 --> 00:02:30,620
‫Now the next thing we can see over in the top, right, these two assets are linked.

44
00:02:30,630 --> 00:02:33,000
‫The behavior tree and the blackboard.

45
00:02:33,000 --> 00:02:35,100
‫What on earth is a blackboard?

46
00:02:35,100 --> 00:02:41,520
‫Well, the blackboard is kind of like the memory of your A.I. It's essentially where you store a bunch

47
00:02:41,520 --> 00:02:42,240
‫of variables.

48
00:02:42,240 --> 00:02:46,650
‫You input them from the gameplay very much like we were doing with the animation system where you had

49
00:02:46,650 --> 00:02:51,150
‫these these different properties that are being set by the gameplay.

50
00:02:51,150 --> 00:02:53,820
‫We do the same thing with the AI.

51
00:02:53,850 --> 00:03:00,180
‫We set lots of properties that give it information about its game world and then the behavior tree draws

52
00:03:00,180 --> 00:03:03,540
‫on those properties and uses them to decide what it's going to do.

53
00:03:03,540 --> 00:03:09,510
‫It can set those properties, it can use them as a memory, it can read from those properties and all

54
00:03:09,540 --> 00:03:10,680
‫that lovely stuff.

55
00:03:10,680 --> 00:03:17,040
‫So with that in mind, how can we set up our behavior tree in blackboard so that they are linked to

56
00:03:17,040 --> 00:03:17,790
‫one another?

57
00:03:18,030 --> 00:03:20,460
‫Well, we're going to expand out in our details pane.

58
00:03:20,460 --> 00:03:26,010
‫You can see here that there is this blackboard asset and automatically it seems to have filled it out

59
00:03:26,010 --> 00:03:32,790
‫for us such that the blackboard asset is linked to this b b b enemy I blackboard asset.

60
00:03:33,000 --> 00:03:38,730
‫Now we are going to be departing from C++ for a little bit to use these assets because obviously behavior

61
00:03:38,730 --> 00:03:42,420
‫trees and blackboards are entirely within the editor.

62
00:03:42,420 --> 00:03:44,460
‫They're not blueprints as such.

63
00:03:44,460 --> 00:03:49,320
‫They are their own editor tools and we can interface with them in C++.

64
00:03:49,320 --> 00:03:53,580
‫But it does mean we're going to be using the editor for the next couple of lectures as well as C++.

65
00:03:53,580 --> 00:03:54,960
‫Not so much blueprint though.

66
00:03:54,960 --> 00:04:00,810
‫So it's great that we've got ourselves this behavior tree and this blackboard, but how on earth do

67
00:04:00,810 --> 00:04:02,400
‫we actually use it?

68
00:04:02,400 --> 00:04:05,580
‫Well, for that, we go back to our AI controller.

69
00:04:05,580 --> 00:04:14,040
‫I if you remember, I controller is entirely responsible for running our behaviors and that is no exception

70
00:04:14,040 --> 00:04:14,640
‫here.

71
00:04:14,820 --> 00:04:19,110
‫The behavior tree is just another thing that the AI controller can use or run.

72
00:04:19,260 --> 00:04:24,810
‫So the way we're going to do this is we're going to go over to our AI control and we need to have a

73
00:04:24,810 --> 00:04:30,090
‫way of referencing in the blueprint what behavior tree we want it to run.

74
00:04:30,600 --> 00:04:35,130
‫So the first thing I'm going to do is I'm actually going to go over to the control CP and comment out

75
00:04:35,130 --> 00:04:40,950
‫the stuff in TTIC because we're going to be replacing this functionality with behavior tree functionality

76
00:04:40,950 --> 00:04:41,640
‫instead.

77
00:04:41,760 --> 00:04:46,440
‫And then that means that I can remove this acceptance radius here because we no longer need it.

78
00:04:46,440 --> 00:04:51,180
‫And instead we're going to be using a pointer to the new behavior.

79
00:04:51,780 --> 00:04:59,100
‫And I have to spell this The American Way Tree and pointer, that's the behavior tree we want there.

80
00:04:59,100 --> 00:05:02,310
‫And I need to forward declare this with a class in front of it.

81
00:05:02,310 --> 00:05:08,910
‫And then basically here, what I want to say is that this is the AI behavior and.

82
00:05:09,770 --> 00:05:13,340
‫I'm going to keep the American spelling here just for consistency.

83
00:05:13,340 --> 00:05:17,630
‫So this is the AI behavior that we want here now to actually run the behavior.

84
00:05:17,630 --> 00:05:18,560
‫It couldn't be simpler.

85
00:05:18,560 --> 00:05:22,700
‫Again, the eye control has a built in function for this.

86
00:05:22,700 --> 00:05:24,500
‫We've got this run behavior.

87
00:05:24,500 --> 00:05:26,390
‫Tree function turns a boolean.

88
00:05:26,390 --> 00:05:30,650
‫Whether you are able to successfully execute it or not, start executing it, that is.

89
00:05:30,650 --> 00:05:35,930
‫And it takes in the beat asset which is appointed to the new behavior tree.

90
00:05:36,290 --> 00:05:40,040
‫So that's what we're going to want to use and we can do this over in begin play.

91
00:05:40,040 --> 00:05:44,270
‫So what we're going to do is, first of all, we're going to check that we indeed have a behavior tree

92
00:05:44,300 --> 00:05:44,660
‫here.

93
00:05:44,660 --> 00:05:47,210
‫So we're going to say I behavior.

94
00:05:48,270 --> 00:05:53,640
‫Is not equal to null pointer because we do not want to be using it otherwise.

95
00:05:53,640 --> 00:05:59,130
‫And then what we can do is we can run the behavior tree called our AI behavior.

96
00:05:59,160 --> 00:06:06,420
‫It really is that simple to get an AI behavior running and we can go ahead and compile.

97
00:06:07,420 --> 00:06:12,790
‫And one more thing we can do with behavior trees is we can kind of see them running at runtime, but

98
00:06:12,790 --> 00:06:15,150
‫to do that, you need to have some sort of node in here.

99
00:06:15,160 --> 00:06:18,910
‫So we're just going to add in a node without fully understanding what it does.

100
00:06:18,910 --> 00:06:23,830
‫I'm going to add in a sequence node and I'm going to hook it up by dragging down from the root node,

101
00:06:23,830 --> 00:06:28,780
‫the bit underneath the root node that highlights when I mouse over it down to the bit above the sequence

102
00:06:28,780 --> 00:06:29,350
‫node.

103
00:06:29,440 --> 00:06:30,850
‫And I'm going to save that.

104
00:06:31,240 --> 00:06:39,700
‫I'm going to go over and hit play and go into my behavior tree asset and you should be able to see a

105
00:06:39,700 --> 00:06:43,420
‫dropdown here, but we're not because it's not currently running.

106
00:06:43,420 --> 00:06:44,530
‫So let's stop playing.

107
00:06:45,010 --> 00:06:46,660
‫And why is it not running?

108
00:06:46,660 --> 00:06:49,360
‫Because we haven't hooked it up in our shooter controller.

109
00:06:49,360 --> 00:06:52,840
‫So let's do that mini challenge for you to hook that up.

110
00:06:53,110 --> 00:06:54,610
‫Hopefully it shouldn't be too hard.

111
00:06:55,060 --> 00:06:56,170
‫Okay, welcome back.

112
00:06:56,170 --> 00:07:01,330
‫So if we go and have a look in here, we're looking for our AI behavior.

113
00:07:01,510 --> 00:07:04,300
‫For some reason, it required a recompile for me to show up for me.

114
00:07:04,300 --> 00:07:04,930
‫But here it is.

115
00:07:04,930 --> 00:07:06,760
‫AI behavior is what we want.

116
00:07:06,790 --> 00:07:07,780
‫We click the drop down.

117
00:07:07,780 --> 00:07:10,630
‫There's only one option at the moment because we've only got one behavior tree.

118
00:07:10,630 --> 00:07:14,200
‫So let's select that one and compile and save.

119
00:07:14,200 --> 00:07:17,380
‫And if we now go ahead and play the game.

120
00:07:18,010 --> 00:07:20,830
‫And go and have a look at our beat enemy.

121
00:07:21,280 --> 00:07:26,140
‫You can see that it's highlighting the node that's currently running and you've got this little drop

122
00:07:26,140 --> 00:07:32,350
‫down next to the pause stop buttons, which tells you which I controller you're currently observing

123
00:07:32,350 --> 00:07:33,130
‫the behavior tree of.

124
00:07:33,160 --> 00:07:38,350
‫So this is a great functionality for you to be able to jump in and go, okay, why is that?

125
00:07:38,350 --> 00:07:39,760
‫I just standing still.

126
00:07:39,760 --> 00:07:41,290
‫Why is he not chasing the player?

127
00:07:41,290 --> 00:07:46,510
‫You can see which part of the behavior he's currently running and go, Ah, that c you know, that thing's

128
00:07:46,510 --> 00:07:47,110
‫gone wrong.

129
00:07:47,140 --> 00:07:48,910
‫It's really helpful for debugging.

130
00:07:48,910 --> 00:07:53,890
‫You can go and have a look at what all your AI in the world are up to by using this dropdown.

131
00:07:54,010 --> 00:07:54,550
‫Great stuff.

132
00:07:54,550 --> 00:07:55,570
‫That's all for this lecture.

133
00:07:55,570 --> 00:08:01,930
‫In the next lecture, we're going to be looking at how we can set information in the blackboard memory

134
00:08:01,930 --> 00:08:04,600
‫of our AI from C++.

135
00:08:04,600 --> 00:08:05,560
‫I'll see you there.

