﻿1
00:00:04,770 --> 00:00:05,940
‫Hello and welcome.

2
00:00:05,940 --> 00:00:11,610
‫In this lecture, we're going to be getting our A.I. to be patrolling back and forth between our locations.

3
00:00:11,610 --> 00:00:17,070
‫So will go to my original start location, then I'll go back to his start location, wait a little bit,

4
00:00:17,070 --> 00:00:18,600
‫and then go again.

5
00:00:18,600 --> 00:00:21,570
‫We're going to be achieving this via behavior trees.

6
00:00:21,570 --> 00:00:23,760
‫So let's dive in and see how it's done.

7
00:00:24,720 --> 00:00:27,750
‫So now we haven't made much use of our behavior tree.

8
00:00:27,750 --> 00:00:28,890
‫Let's fix that.

9
00:00:28,890 --> 00:00:36,570
‫We're going to create a behavior that allows us to go from the place where we're starting to the player's

10
00:00:36,570 --> 00:00:42,570
‫location and then go back to our original starting location.

11
00:00:42,930 --> 00:00:46,140
‫Now, the way we're going to do this is using the following.

12
00:00:46,140 --> 00:00:53,190
‫So there is a distinction in behavior trees between things called tasks and things called composite

13
00:00:53,190 --> 00:00:57,120
‫nodes, or in this case, the example of the composite node is a sequence.

14
00:00:57,120 --> 00:01:01,320
‫So let's understand sequences and tasks as they kind of sound.

15
00:01:01,530 --> 00:01:03,330
‫A task is something you do.

16
00:01:03,450 --> 00:01:09,030
‫A sequence is a sequence of such tasks and it will run all of the tasks in order.

17
00:01:09,660 --> 00:01:15,690
‫So if we right click and expand out the tasks here, you can see an example of what could be used as

18
00:01:15,690 --> 00:01:16,320
‫a task.

19
00:01:16,470 --> 00:01:21,630
‫For example, we can have move two, which moves to a location that's moved directly to as well.

20
00:01:21,630 --> 00:01:23,700
‫I'm not entirely sure of the distinction we're going to be using.

21
00:01:23,700 --> 00:01:24,390
‫Move two.

22
00:01:24,420 --> 00:01:27,660
‫You can get it to play an animation such as the scratch your nose animation.

23
00:01:27,660 --> 00:01:30,990
‫You can get them to play a sound like, Hey you, that sort of stuff.

24
00:01:30,990 --> 00:01:34,770
‫You can get it to run another animal behavior if you want to.

25
00:01:35,130 --> 00:01:39,690
‫There's also a weight here which is very useful for adding in some pauses into your behavior.

26
00:01:39,930 --> 00:01:46,080
‫Now what's interesting about these tasks is that they can run for multiple frames, for multiple ticks.

27
00:01:46,080 --> 00:01:53,250
‫So Move two will be running the entire time that you are moving to a location and it'll only finish

28
00:01:53,250 --> 00:01:54,540
‫when you get there.

29
00:01:54,930 --> 00:02:02,520
‫So if we did a move too and we had, we can stick it in a sequence here by just dragging down from the

30
00:02:02,520 --> 00:02:03,690
‫sequence node.

31
00:02:03,930 --> 00:02:08,880
‫Then what you're going to get is, first of all, it's going to try and move to the destination.

32
00:02:09,000 --> 00:02:15,570
‫And then we could add in a second task node which tells it to wait, and then we can add in a third

33
00:02:15,570 --> 00:02:16,530
‫task task node.

34
00:02:16,530 --> 00:02:21,120
‫Notice I'm pulling down from the sequence here and releasing, which allows me to add in a task quite

35
00:02:21,120 --> 00:02:23,340
‫quickly underneath that sequence node.

36
00:02:23,340 --> 00:02:24,060
‫And then I can tell it.

37
00:02:24,060 --> 00:02:26,580
‫I want to move to another location and then I can tell it.

38
00:02:26,580 --> 00:02:29,880
‫I want to wait again for a certain amount of time.

39
00:02:29,880 --> 00:02:35,850
‫Now this is the basics of that moving to the player and then moving back to where we started from.

40
00:02:35,850 --> 00:02:38,460
‫And you can see how the sequence node is working here.

41
00:02:38,460 --> 00:02:40,080
‫Now there's a route node as well.

42
00:02:40,080 --> 00:02:44,460
‫The route node can only have one child node, so don't try and add more things under that.

43
00:02:44,460 --> 00:02:49,800
‫So we've got one sequence node is the route here and that's basically saying that I'm going to run all

44
00:02:49,800 --> 00:02:55,740
‫of these tasks in order until one of them fails and then once we're finished, we can start the process

45
00:02:55,740 --> 00:02:56,310
‫again.

46
00:02:57,090 --> 00:03:02,010
‫So the other thing to note here is that if you click on any one of these nodes, the details over here

47
00:03:02,010 --> 00:03:05,580
‫give us some impact over how that node works.

48
00:03:05,700 --> 00:03:13,440
‫For example, our move too currently takes a blackboard key in the blackboard section of its details

49
00:03:13,440 --> 00:03:17,340
‫pane of self active, but we can change that over to the player location.

50
00:03:17,340 --> 00:03:22,380
‫So that's basically saying this move to Node will now move to our player location.

51
00:03:22,380 --> 00:03:27,480
‫Then we're going to wait 5 seconds by default, but we can change that again to say we might want to

52
00:03:27,480 --> 00:03:34,170
‫wait 2 seconds and you might want to have a random deviation of 1/2 so that basically it will be waiting

53
00:03:34,170 --> 00:03:36,450
‫somewhere between 1/2 and 3 seconds.

54
00:03:36,690 --> 00:03:41,610
‫Then we want to move to a different location, and that's a location that we don't currently have,

55
00:03:41,610 --> 00:03:43,290
‫which is our start location.

56
00:03:43,380 --> 00:03:44,760
‫So we can go ahead and add that.

57
00:03:44,760 --> 00:03:51,780
‫Let's go over to our blackboard, add a new key, add a vector and have that as our start location.

58
00:03:52,110 --> 00:03:56,820
‫And then we can go back over to the behavior tree and see that our move to a node, if you go to the

59
00:03:56,820 --> 00:04:03,030
‫details pane now has the start location as an option and then we might say we want to wait 5 seconds

60
00:04:03,030 --> 00:04:07,320
‫and then the process will start again because we'll have successfully completed our sequence node.

61
00:04:07,320 --> 00:04:08,430
‫So this is how it works.

62
00:04:08,430 --> 00:04:13,560
‫You step down into the sequence node, the sequence node then says, okay, well where am I at?

63
00:04:13,560 --> 00:04:15,570
‫I want to start at the beginning.

64
00:04:15,570 --> 00:04:16,860
‫And you can see this number.

65
00:04:16,860 --> 00:04:22,650
‫Things here are telling us the execution order, we start at zero, which is the sequence node one is

66
00:04:22,650 --> 00:04:29,220
‫the move to then two is a weight three it's moved to four, etc. So we step through doing each task

67
00:04:29,220 --> 00:04:30,780
‫until completion or failure.

68
00:04:30,780 --> 00:04:34,050
‫If we fail, then the sequence node will fail as a whole.

69
00:04:34,050 --> 00:04:40,020
‫None of these are likely to fail, however, so we'll go through the whole thing and then the sequence

70
00:04:40,020 --> 00:04:42,660
‫node will complete and the route will start it again.

71
00:04:42,660 --> 00:04:47,430
‫So we're going to go through in a loop, going to the player location weighting, going back to the

72
00:04:47,430 --> 00:04:49,860
‫start location, waiting for a little bit longer.

73
00:04:49,980 --> 00:04:54,000
‫So that's the power of behavior trees and you can start to nest these deeper.

74
00:04:54,000 --> 00:04:58,380
‫You can have choices in here, you can have decorators, all the stuff that we're going to see in upcoming

75
00:04:58,380 --> 00:04:58,920
‫lectures.

76
00:04:58,920 --> 00:05:00,810
‫But this is the basis of it.

77
00:05:00,810 --> 00:05:07,530
‫A series of instructions that your AI can run and they can take more than one frame to complete.

78
00:05:07,530 --> 00:05:13,380
‫Now what we want to do is actually set this start location to be something meaningful.

79
00:05:13,620 --> 00:05:16,650
‫Now that should be reasonably straightforward.

80
00:05:16,650 --> 00:05:22,590
‫The only thing I probably want to tell you how to do is how do we get hold of the -- that we're in

81
00:05:22,590 --> 00:05:23,460
‫control of?

82
00:05:23,460 --> 00:05:24,060
‫So just.

83
00:05:24,140 --> 00:05:32,960
‫A reminder that if we go over to the controller h here, then we can have a look for the get -- and

84
00:05:32,960 --> 00:05:40,160
‫the get -- method for both player controllers and air controllers allows us to get hold of the --

85
00:05:40,160 --> 00:05:41,240
‫that is being controlled.

86
00:05:41,240 --> 00:05:49,160
‫So you can use this to get the -- being controlled and get its location at begin play in order to

87
00:05:49,160 --> 00:05:51,700
‫set our start location.

88
00:05:51,710 --> 00:05:53,240
‫So here's the challenge to you.

89
00:05:53,240 --> 00:05:59,060
‫Set a start location, get the --'s location as we've just talked a little bit about, set the correct

90
00:05:59,060 --> 00:06:04,250
‫blackboard key, use the player position that we've already used as a template for that.

91
00:06:04,610 --> 00:06:09,680
‫Set up the behavior tree the way I have in the lecture and test it out.

92
00:06:09,680 --> 00:06:17,480
‫See if that behavior gets the desired behavior of the player or the enemy moving to the player's --

93
00:06:17,480 --> 00:06:19,910
‫and then back to its original starting location.

94
00:06:19,910 --> 00:06:21,860
‫Pause the video and have a go.

95
00:06:23,000 --> 00:06:23,270
‫Okay.

96
00:06:23,270 --> 00:06:23,900
‫Welcome back.

97
00:06:23,900 --> 00:06:25,310
‫So hopefully that worked out for you.

98
00:06:25,310 --> 00:06:27,050
‫Let's give this a shot.

99
00:06:27,050 --> 00:06:31,190
‫So I'm going to copy the line for our player location.

100
00:06:31,250 --> 00:06:35,480
‫And instead of setting the player location, we go over to our blackboard, have a look at our start

101
00:06:35,480 --> 00:06:36,290
‫location.

102
00:06:36,290 --> 00:06:39,770
‫We're going to copy that key value in here.

103
00:06:40,100 --> 00:06:45,530
‫And then instead of getting the location of the player --, we're going to do a get -- and then

104
00:06:45,530 --> 00:06:52,640
‫get actor location, which is going to get the -- that we're currently controlling and get its location

105
00:06:52,640 --> 00:06:58,370
‫in the world so we can go ahead and go back to the editor and compile this.

106
00:06:58,370 --> 00:07:03,170
‫And once that's done, hit play and have a look at our --.

107
00:07:03,170 --> 00:07:05,390
‫He comes over, moves towards us.

108
00:07:05,390 --> 00:07:10,780
‫It waits 3 seconds or 2 seconds or 1/2, then goes back to his original start location.

109
00:07:10,830 --> 00:07:16,880
‫Then he waits 5 seconds, and then he comes back over to that original location of the player --.

110
00:07:16,880 --> 00:07:18,530
‫So he's not updating that every frame.

111
00:07:18,560 --> 00:07:19,040
‫That's okay.

112
00:07:19,070 --> 00:07:20,960
‫That's how we designed it at the moment.

113
00:07:20,960 --> 00:07:23,360
‫But you can see the behavior going on.

114
00:07:23,360 --> 00:07:25,520
‫He's patrolling back and forth.

115
00:07:25,520 --> 00:07:29,240
‫So let's have a look at the all of this in action.

116
00:07:29,240 --> 00:07:30,680
‫Have a look at our behavior tree.

117
00:07:30,860 --> 00:07:34,940
‫If we go over to our behavior tree and select the I in question, you can see where he's at.

118
00:07:34,970 --> 00:07:37,610
‫He's moving to he's waiting so many seconds.

119
00:07:37,610 --> 00:07:39,470
‫Then he's moving to his start location.

120
00:07:39,470 --> 00:07:46,490
‫He's waiting again so we can undock this and see both of these things happening at the same time in

121
00:07:46,490 --> 00:07:47,000
‫our game.

122
00:07:47,000 --> 00:07:49,040
‫You can see he's moving to that location.

123
00:07:49,040 --> 00:07:52,880
‫He's waiting now he is back to moving it to his start location.

124
00:07:52,880 --> 00:07:59,000
‫So this is really handy for us to be able to debug, especially when these behaviors get a little bit

125
00:07:59,000 --> 00:07:59,690
‫more complicated.

126
00:07:59,690 --> 00:08:06,290
‫And it also gives you a sense of how unreal is calculating the behavior tree and how that execution

127
00:08:06,290 --> 00:08:09,530
‫flow is working within our behavior tree.

128
00:08:09,710 --> 00:08:10,340
‫Great stuff.

129
00:08:10,340 --> 00:08:15,050
‫In the next lecture, we're going to make these behaviors even more complicated and add some element

130
00:08:15,050 --> 00:08:21,620
‫of decision to whether we choose to follow a player or whether we choose to stay put where we are.

131
00:08:21,770 --> 00:08:23,330
‫I'll see you in that lecture.

