﻿1
00:00:04,280 --> 00:00:05,540
‫Hello and welcome.

2
00:00:05,540 --> 00:00:09,140
‫In this lecture, we've got our eye that is following us around the world.

3
00:00:09,140 --> 00:00:11,570
‫It will go and investigate when he loses sight of me.

4
00:00:11,570 --> 00:00:13,730
‫So it's quite hard to lose.

5
00:00:13,730 --> 00:00:20,120
‫But if I go too many corners away, he's not going to be able to keep up and eventually he's going to

6
00:00:20,120 --> 00:00:25,160
‫lose interest and go back to his start location over there.

7
00:00:25,370 --> 00:00:31,010
‫That's what we're going to be implementing in this lecture by finishing off our behavior tree task.

8
00:00:31,010 --> 00:00:32,930
‫Let's dive in and see how it's done.

9
00:00:33,550 --> 00:00:38,380
‫So we want to get our task to execute, to actually do something.

10
00:00:38,380 --> 00:00:42,400
‫And particularly we want to be able to clear that blackboard value, do what it says on the tin.

11
00:00:42,820 --> 00:00:44,830
‫So how do we go about doing that?

12
00:00:44,860 --> 00:00:49,210
‫Well, for that, we need to go up to our base class and have a look at what's implemented there.

13
00:00:49,210 --> 00:00:56,350
‫In fact, we need to go up one level higher, which, as you can see from the inheritance of the task

14
00:00:56,890 --> 00:00:57,880
‫blackboard base.

15
00:00:57,880 --> 00:01:00,540
‫Above that we have the BT task node.

16
00:01:00,550 --> 00:01:05,320
‫So let's use F 12 to navigate to the implementation there.

17
00:01:05,320 --> 00:01:12,700
‫And if you scroll up to the top of the BT task node header file, you can see there's the information

18
00:01:12,700 --> 00:01:13,720
‫about this class.

19
00:01:13,720 --> 00:01:18,670
‫And the most important thing is that these are the virtual functions that we're most likely to want

20
00:01:18,670 --> 00:01:19,960
‫to implement.

21
00:01:20,260 --> 00:01:22,510
‫So the first one is execute task.

22
00:01:22,510 --> 00:01:30,760
‫This is called when the task starts to execute, we then have a board task, which is when it's decided

23
00:01:30,760 --> 00:01:36,100
‫that we need to stop executing, for example, because a condition has become false and we've got that

24
00:01:36,100 --> 00:01:37,060
‫signalling set up.

25
00:01:37,060 --> 00:01:38,710
‫So it aborts this task.

26
00:01:39,040 --> 00:01:43,720
‫Tick task happens all the time that the task is executing, but not the first tick.

27
00:01:43,720 --> 00:01:46,660
‫So the first tick will call execute task thereafter.

28
00:01:46,660 --> 00:01:49,540
‫We'll keep calling tick task every time.

29
00:01:50,140 --> 00:01:56,200
‫And that's for behaviors that might run for longer than one tick, such as moving to an enemy might

30
00:01:56,200 --> 00:01:57,400
‫take multiple ticks.

31
00:01:57,400 --> 00:01:59,710
‫So tick task allows us to do something.

32
00:01:59,710 --> 00:02:05,290
‫Every tick and on message is for other things other than aborting that might send a message to this

33
00:02:05,290 --> 00:02:05,860
‫task.

34
00:02:05,860 --> 00:02:09,250
‫We don't really have to worry about this particular function.

35
00:02:09,250 --> 00:02:15,370
‫So the first three are really the ones we're more interested in, and seeing as our task is simply just

36
00:02:15,370 --> 00:02:21,370
‫going to clear the blackboard value as soon as it is executed and then it's going to finish straightaway.

37
00:02:21,370 --> 00:02:24,100
‫It doesn't need to do things across multiple ticks.

38
00:02:24,100 --> 00:02:30,190
‫We're interested in the execute task, so if we search in this header file for execute task, you can

39
00:02:30,190 --> 00:02:37,420
‫see the full signature is here and the return type here is this boot node result.

40
00:02:38,080 --> 00:02:43,660
‫And it takes in two things the owner component and the node memory.

41
00:02:43,720 --> 00:02:48,880
‫Now the reason it takes in node memory is a little bit complicated, but the important thing to know

42
00:02:48,880 --> 00:02:58,930
‫here is that a task class or a task class instance, there's only one of them per all of the blackboards.

43
00:02:58,930 --> 00:03:04,960
‫Essentially, they get shared between everything, which is why if you want to save some information

44
00:03:04,960 --> 00:03:12,400
‫that's related to a node in one particular behavior tree, then what you need to do is store it in this

45
00:03:12,400 --> 00:03:13,660
‫memory pointer.

46
00:03:13,660 --> 00:03:16,210
‫But we're not going to be doing anything related to that.

47
00:03:16,210 --> 00:03:18,670
‫It's a little bit more advanced to need to do that.

48
00:03:18,670 --> 00:03:23,860
‫And there are other ways to get around this, such as allowing Unreal to create multiple instances for

49
00:03:23,860 --> 00:03:28,450
‫every behavior tree, which might be a little bit neater or not, but this is a slightly more efficient

50
00:03:28,450 --> 00:03:30,430
‫way and we don't have to worry about it.

51
00:03:30,430 --> 00:03:35,620
‫Ultimately, what we're interested in is the fact that we can get to our own a component, the behavior

52
00:03:35,620 --> 00:03:40,930
‫tree component, and from there we can get to other things such as the pawn, etc. But that's also the

53
00:03:40,930 --> 00:03:43,270
‫subject of another lecture.

54
00:03:43,270 --> 00:03:48,910
‫So what's interesting and important now is just to implement our execute task.

55
00:03:48,910 --> 00:03:55,450
‫So we're going to copy the signature, go over to our clear blackboard value H and let's just see as

56
00:03:55,450 --> 00:04:00,040
‫this public note, I think this is going to be protected because it's in the default section.

57
00:04:00,040 --> 00:04:04,930
‫So we're going to create ourselves a protected section and paste it in there and then we're going to

58
00:04:04,930 --> 00:04:08,500
‫go ahead and create the or put an override just to make sure be safe.

59
00:04:08,500 --> 00:04:14,500
‫And then we're going to create an implementation over in the C++, remembering that we always want to

60
00:04:14,500 --> 00:04:21,520
‫be calling to super when we're overriding so super execute task with the owner component and the node

61
00:04:21,520 --> 00:04:24,460
‫memory being called up to our parent here.

62
00:04:24,460 --> 00:04:30,790
‫And then what we want to do is get hold of our blackboard so that we can clear the key.

63
00:04:30,790 --> 00:04:32,230
‫And there's two things that are important here.

64
00:04:32,230 --> 00:04:35,140
‫First of all, how do we know what Key was selected?

65
00:04:35,140 --> 00:04:41,230
‫Well, if we go to our Blackboard Base H, you can see that one of the methods there is get selected

66
00:04:41,230 --> 00:04:42,340
‫Blackboard Key.

67
00:04:42,340 --> 00:04:48,220
‫So you can easily just call this in your C++, get selected Blackboard Key, and that's going to return.

68
00:04:48,220 --> 00:04:54,100
‫If we hover over it, it returns an F name, the F name of the blackboard key that was selected in the

69
00:04:54,100 --> 00:04:54,730
‫behavior tree.

70
00:04:54,730 --> 00:04:59,380
‫Now, the other thing we need to do is to be actually actually able to get hold of the blackboard related

71
00:04:59,380 --> 00:05:00,280
‫to this behavior tree.

72
00:05:00,280 --> 00:05:02,380
‫So for that, we've got our own a comp.

73
00:05:02,380 --> 00:05:07,990
‫Now notice here that the type of owner comp being passed in here is actually a reference, not a pointer.

74
00:05:07,990 --> 00:05:09,850
‫So it can't be null for a start.

75
00:05:09,850 --> 00:05:10,720
‫That's great.

76
00:05:10,720 --> 00:05:15,730
‫But it also means that we need to use the dot operator rather than the the arrow operator because we're

77
00:05:15,730 --> 00:05:21,910
‫not using a pointer and from here we can simply get blackboard component.

78
00:05:21,910 --> 00:05:26,260
‫So that just gets the blackboard component if you hover over it, this one is a pointer to a blackboard

79
00:05:26,260 --> 00:05:29,470
‫component, so you will need to use arrow operators.

80
00:05:29,470 --> 00:05:32,680
‫So we already know how to clear keys on blackboard.

81
00:05:32,880 --> 00:05:34,590
‫Opponents, that's going to be your challenge.

82
00:05:34,590 --> 00:05:36,690
‫So hopefully the challenge is not too complicated.

83
00:05:36,690 --> 00:05:38,620
‫You want to clear the selected key.

84
00:05:38,800 --> 00:05:41,730
‫We you can check where we've done this before.

85
00:05:42,240 --> 00:05:44,910
‫Do you need any includes in order to do this?

86
00:05:44,910 --> 00:05:53,010
‫Maybe the blackboard needs to include use the key selected in your behaviour tree as the key that we

87
00:05:53,010 --> 00:05:53,550
‫want to clear.

88
00:05:53,550 --> 00:05:55,530
‫So pause the video and have a go.

89
00:05:55,560 --> 00:05:56,670
‫Okay, welcome back.

90
00:05:56,670 --> 00:05:58,740
‫So let's give this a shot.

91
00:05:58,740 --> 00:06:01,560
‫So the first thing is we've got our blackboard component.

92
00:06:01,560 --> 00:06:03,060
‫Let's use the error operator here.

93
00:06:03,360 --> 00:06:04,560
‫Nothing is coming up.

94
00:06:04,560 --> 00:06:11,910
‫So I'm assuming that I need and indeed there's some red squiggles I need a hash include here to do the

95
00:06:11,910 --> 00:06:15,960
‫behavior tree and then the blackboard component here and now.

96
00:06:15,960 --> 00:06:21,300
‫Hopefully my autocomplete should be working, which will allow me to have the clear.

97
00:06:21,300 --> 00:06:23,580
‫Let's just try and refresh our memories.

98
00:06:23,580 --> 00:06:25,230
‫Where have we done this before?

99
00:06:25,230 --> 00:06:30,810
‫If we go over to Shooter I, Controller CP and have a look at what we've got here, we've got clear

100
00:06:30,810 --> 00:06:31,500
‫value.

101
00:06:31,500 --> 00:06:32,790
‫That's the one we're looking for.

102
00:06:32,790 --> 00:06:35,430
‫So let's try that clear value.

103
00:06:35,430 --> 00:06:36,600
‫And what does it take in?

104
00:06:36,600 --> 00:06:41,370
‫It takes in either an F key or an F name, great f name we can get.

105
00:06:41,370 --> 00:06:46,830
‫And where did we say we could get that with the get selected blackboard key that is on this class because

106
00:06:46,830 --> 00:06:48,030
‫it's in the parent class.

107
00:06:48,030 --> 00:06:51,300
‫So get selected Blackboard Key and it's as simple as that.

108
00:06:51,300 --> 00:06:53,910
‫We're clearing the value of the selected Blackboard Key.

109
00:06:53,910 --> 00:06:55,590
‫Now, one final thing we need to do.

110
00:06:55,590 --> 00:06:57,060
‫This wasn't part of your challenge, so don't worry.

111
00:06:57,060 --> 00:07:03,240
‫If you didn't know this is that we need to return because there is a return value for our clear blackboard

112
00:07:03,240 --> 00:07:04,680
‫value execute task.

113
00:07:04,680 --> 00:07:06,810
‫And the return value is quite interesting.

114
00:07:06,810 --> 00:07:07,410
‫Let's have a look.

115
00:07:07,410 --> 00:07:12,600
‫Let's follow it with an F 12 and see what different status is we can return from our execute test.

116
00:07:12,600 --> 00:07:14,400
‫Now the first one is succeeded.

117
00:07:14,400 --> 00:07:14,760
‫Great.

118
00:07:14,760 --> 00:07:19,710
‫That basically signals to the behaviour tree that this node is finished executing and we can move on

119
00:07:19,710 --> 00:07:20,910
‫to the next one.

120
00:07:20,910 --> 00:07:21,930
‫Failed again.

121
00:07:21,930 --> 00:07:27,450
‫This is a finished so we can move on to the next one, but this time it's saying that it failed.

122
00:07:27,450 --> 00:07:34,500
‫So that gives the different indications to, for example, as sequence nodes or our selected nodes as

123
00:07:34,500 --> 00:07:36,750
‫to what those nodes should actually be doing.

124
00:07:37,260 --> 00:07:44,850
‫Then we have got aborted, which is basically the same as failure and we have in progress.

125
00:07:44,850 --> 00:07:51,420
‫If you return in progress from an execute task, it's basically going to say you've got to keep calling

126
00:07:51,420 --> 00:07:57,120
‫tick until I tell you otherwise, until I say that I have finished now we're not going to be using this.

127
00:07:57,120 --> 00:08:02,700
‫But it's good to keep in mind that if you need to create a task that is long running, we'll run across

128
00:08:02,700 --> 00:08:03,660
‫multiple ticks.

129
00:08:03,660 --> 00:08:06,060
‫Then we're going to have to return in progress.

130
00:08:06,060 --> 00:08:08,220
‫But as I said, we want succeeded.

131
00:08:08,400 --> 00:08:10,260
‫So let's go in and do that.

132
00:08:10,260 --> 00:08:14,490
‫Return EB Beat node result colon, colon.

133
00:08:14,490 --> 00:08:20,550
‫And it is going to be succeeded because we succeeded to clear that value basically no matter what.

134
00:08:20,760 --> 00:08:25,470
‫So we can go ahead and save all of this, go over into the editor and hit compile.

135
00:08:25,470 --> 00:08:31,710
‫And when that's complete, let's go ahead and open up our AI and the behavior tree.

136
00:08:31,740 --> 00:08:33,020
‫So where's that one beat?

137
00:08:33,030 --> 00:08:35,490
‫And I dock it to the main window and let's have a look.

138
00:08:35,490 --> 00:08:40,800
‫We've got a clear blackboard value which should now be able to allow us to move to the last known location,

139
00:08:40,800 --> 00:08:47,250
‫clear the blackboard value, wait for five, and then we should see that investigating this last known

140
00:08:47,250 --> 00:08:52,200
‫player location should be unset, so we should move back to our start location.

141
00:08:52,200 --> 00:08:56,790
‫So let's see if this behavior is now working now that the task is fully implement.

142
00:08:56,800 --> 00:09:00,450
‫So he's coming over to me, he's investigating me.

143
00:09:00,480 --> 00:09:01,800
‫I'm going to go and hide.

144
00:09:01,800 --> 00:09:03,000
‫So let's go and hide.

145
00:09:03,150 --> 00:09:04,200
‫He's coming to investigate.

146
00:09:04,200 --> 00:09:05,550
‫Let's go and hide again.

147
00:09:05,760 --> 00:09:07,290
‫So go and hide.

148
00:09:07,830 --> 00:09:10,080
‫Let's see if we can spot him over there.

149
00:09:10,080 --> 00:09:16,020
‫He should be waiting at the moment and then he's going back to his start location, which unfortunately

150
00:09:16,020 --> 00:09:18,420
‫makes him intersect with me.

151
00:09:18,420 --> 00:09:25,350
‫So let's see if this is actually working as we expect, by bringing out our enemy a window and docking

152
00:09:25,350 --> 00:09:29,820
‫it over to the right and putting our main window over on the left.

153
00:09:29,820 --> 00:09:34,890
‫It's a little bit cramped, but we can we can make do and then we're going to try and escape this guy

154
00:09:34,890 --> 00:09:37,800
‫one more time and see what happens with his behavior.

155
00:09:37,800 --> 00:09:41,910
‫You can see he is moving to the last known player location.

156
00:09:42,510 --> 00:09:45,990
‫And now he is waiting, having cleared the value.

157
00:09:45,990 --> 00:09:51,360
‫And then you can see he's going to move to and you can see these red bars on the decorators are telling

158
00:09:51,360 --> 00:09:54,030
‫us that neither of these two conditions are true.

159
00:09:54,030 --> 00:09:55,950
‫So he can't do either of those.

160
00:09:55,950 --> 00:09:57,840
‫Finally, he's moved to the start location.

161
00:09:57,840 --> 00:10:01,170
‫So now it's flickering because basically it can't do anything.

162
00:10:01,320 --> 00:10:09,810
‫So that basically is that this lecture we've seen how we can get the tasks to execute code understood

163
00:10:09,810 --> 00:10:14,910
‫the different statuses that you can return from execute task how to get the blackboard from the task

164
00:10:14,910 --> 00:10:19,890
‫and also how to get the currently selected key on a blackboard task.

165
00:10:19,890 --> 00:10:26,310
‫In the next lecture, we're going to see how we can get hold of the world outside of the blackboard

166
00:10:26,310 --> 00:10:30,450
‫behavior tree, how we can get hold of the pawn from within a behavior tree task.

167
00:10:30,450 --> 00:10:31,080
‫I'll see you there.

