﻿1
00:00:04,260 --> 00:00:05,070
‫Hello and welcome.

2
00:00:05,070 --> 00:00:08,490
‫In this lecture we're going to be working on AI Focus.

3
00:00:08,490 --> 00:00:14,850
‫So that is some built in functionality in Unreal that we can use to make sure that our AI characters

4
00:00:14,850 --> 00:00:19,560
‫follow us around the world are able to aim at us no matter where we go.

5
00:00:19,560 --> 00:00:22,080
‫Let's dive in and see how focus works.

6
00:00:23,190 --> 00:00:26,220
‫So I currently doesn't really do very much.

7
00:00:26,220 --> 00:00:33,000
‫And I think a first starting point for it to do anything would be for it to turn and face towards our

8
00:00:33,000 --> 00:00:33,690
‫player.

9
00:00:33,990 --> 00:00:40,640
‫Now to do this, we can use some built in functionality of the AI, which is setting a focal point.

10
00:00:40,650 --> 00:00:46,890
‫So if you have a look at the AI control and look for set focus, there's a couple of relevant methods

11
00:00:46,890 --> 00:00:47,120
‫here.

12
00:00:47,130 --> 00:00:52,380
‫One is set focal points, the other is set focus, and the one just below that is clear focus.

13
00:00:52,770 --> 00:00:59,340
‫So the idea here is that you can tell the AI controller that it should be trying to focus either on

14
00:00:59,340 --> 00:01:03,830
‫a particular point or it should be trying to focus on a particular actor.

15
00:01:03,840 --> 00:01:08,910
‫If you choose the latter, if you choose to focus on an actor, then what's going to happen is as that

16
00:01:08,910 --> 00:01:12,540
‫actor moves, you're going to keep focusing on that actor.

17
00:01:12,540 --> 00:01:17,310
‫Whereas if you chose to focus on a particular point, you would keep focusing on that point.

18
00:01:17,310 --> 00:01:23,600
‫But if that point was the location of an actor, when that actor moves, that point does not get updated.

19
00:01:23,610 --> 00:01:28,620
‫So typically focal points are for things that are fixed in the world, actors for things that move around

20
00:01:28,620 --> 00:01:29,700
‫that kind of makes sense.

21
00:01:29,700 --> 00:01:35,970
‫And then we have this clear focus function down here, which tells us that we should stop focusing on

22
00:01:35,970 --> 00:01:36,360
‫something.

23
00:01:36,360 --> 00:01:37,110
‫So that's how it works.

24
00:01:37,110 --> 00:01:39,030
‫You set a focus, you clear a focus.

25
00:01:39,120 --> 00:01:45,090
‫Now there is this priority variable in here which we are going to come to a little bit.

26
00:01:45,090 --> 00:01:48,360
‫But the idea here is that you've got different levels of priority.

27
00:01:48,360 --> 00:01:55,470
‫We've got this enum here which by default on set focus and set focal point is set to gameplay.

28
00:01:55,470 --> 00:02:02,460
‫If we go and have a look at the definition of that enum, you can see there is a default move and gameplay

29
00:02:02,460 --> 00:02:05,340
‫and they're basically listed in the order of priority.

30
00:02:05,340 --> 00:02:07,500
‫So gameplay will have the highest priority.

31
00:02:07,500 --> 00:02:09,960
‫If you set something as gameplay, that's what it's going to look at.

32
00:02:09,960 --> 00:02:15,690
‫If gameplay isn't set because you cleared the gameplay priority, then it's going to look where it's

33
00:02:15,690 --> 00:02:22,230
‫going and if it's not moving anywhere, then it's going to just look at the default priority, which

34
00:02:22,230 --> 00:02:24,210
‫might just be somewhere off in the distance.

35
00:02:24,210 --> 00:02:29,490
‫So if you've got a guard that's got a patrolling behavior, he'll default to looking to where he's next

36
00:02:29,490 --> 00:02:31,290
‫moving to once he gets there.

37
00:02:31,290 --> 00:02:34,140
‫You could set a default point for him to look at.

38
00:02:34,140 --> 00:02:40,260
‫So, I don't know, maybe he's looking at the entry way to his particular zone or something like that.

39
00:02:40,260 --> 00:02:43,020
‫Anyway, this is what the priorities are all about.

40
00:02:43,020 --> 00:02:47,460
‫As I say, we won't have to be using them too much because we're mostly going to be using this gameplay

41
00:02:47,460 --> 00:02:51,840
‫functionality to say that, Hey, there's something interesting going on over there.

42
00:02:51,840 --> 00:02:54,690
‫There's the player, so let's go ahead and look at him.

43
00:02:54,960 --> 00:03:00,630
‫So the first thing we're going to do is we're going to implement a begin play on our AI controller so

44
00:03:00,630 --> 00:03:02,820
‫that we can set the focus on Begin Play.

45
00:03:02,820 --> 00:03:05,880
‫This is just going to be to show us how this function works.

46
00:03:05,880 --> 00:03:09,810
‫So Begin Play, as we've probably seen before, it's a protected function.

47
00:03:09,810 --> 00:03:15,150
‫So we need to create a protected section and then we want our virtual void begin play.

48
00:03:15,150 --> 00:03:20,970
‫We're going to put the override keyword just for safety here and then we're going to go ahead and create

49
00:03:20,970 --> 00:03:23,130
‫the implementation over in the C++.

50
00:03:23,250 --> 00:03:28,650
‫And we're going to want to call to super begin play because that's always good practice because otherwise

51
00:03:28,650 --> 00:03:30,030
‫things in blueprints go wrong.

52
00:03:30,030 --> 00:03:35,310
‫If you forget to do this, if you ever have a problem with blueprints after you've created a begin play,

53
00:03:35,310 --> 00:03:37,380
‫make sure you check that you're calling to super.

54
00:03:37,710 --> 00:03:44,730
‫And then at this point, what we want to do is get hold of the pawn actor and set the focus to it.

55
00:03:45,930 --> 00:03:52,710
‫To get hold of a pawn actor, we can use a function from the gameplay statics header which if you hover

56
00:03:52,710 --> 00:03:58,170
‫over this or search in control p to find gameplay statics, you can find that this is under the kismet

57
00:03:58,170 --> 00:03:59,940
‫forward slash gameplay statics.

58
00:03:59,940 --> 00:04:04,530
‫So we'll go back over to the shooter I controller CP and include that.

59
00:04:04,530 --> 00:04:11,730
‫So hash include and then it's going to be kismet forward slash gameplay statics and then we should be

60
00:04:11,730 --> 00:04:19,530
‫able to do our U gameplay statics and use a function from within here which I find very useful is the

61
00:04:19,530 --> 00:04:24,270
‫get player pawn, which is going to return as a pointer to a player pawn.

62
00:04:24,270 --> 00:04:29,190
‫And it does that by taking in a world context object and the player index.

63
00:04:29,190 --> 00:04:31,560
‫In our case we're going to find the zero player.

64
00:04:31,650 --> 00:04:36,150
‫So let's get play a pawn and do a get world.

65
00:04:36,150 --> 00:04:42,060
‫And then we're going to look for index zero and we're going to save this into a variable, a pawn.

66
00:04:42,890 --> 00:04:46,040
‫At which is going to be the player -- like.

67
00:04:46,040 --> 00:04:48,050
‫So that's what we want to focus on.

68
00:04:49,990 --> 00:04:53,250
‫So it's challenge time for you to set the focus.

69
00:04:53,260 --> 00:04:58,930
‫Use the player -- that we've just got using our gameplay statics function call, set, focus and test

70
00:04:58,930 --> 00:05:03,370
‫it out in the world to see if you move around our different characters.

71
00:05:03,370 --> 00:05:04,870
‫Will they follow you?

72
00:05:04,870 --> 00:05:06,150
‫Will their gaze follow you?

73
00:05:06,160 --> 00:05:07,840
‫Pause video and find out.

74
00:05:08,900 --> 00:05:09,200
‫Okay.

75
00:05:09,200 --> 00:05:09,880
‫Welcome back.

76
00:05:09,890 --> 00:05:11,990
‫So hopefully it wasn't too difficult.

77
00:05:11,990 --> 00:05:16,850
‫We need to call set focus because set focus is on a parent.

78
00:05:16,850 --> 00:05:19,310
‫We don't have to find any pointers here.

79
00:05:19,310 --> 00:05:22,640
‫We do set focus and what have we got here?

80
00:05:22,640 --> 00:05:28,040
‫We just need to put in the actor basically because the priority is already got a default of being set

81
00:05:28,040 --> 00:05:33,830
‫to the gameplay, which is what we wanted anyway, so we can just pass in our player pawn and that should

82
00:05:33,830 --> 00:05:34,940
‫be who we focus on.

83
00:05:34,940 --> 00:05:36,170
‫So let's go ahead and build.

84
00:05:37,440 --> 00:05:42,750
‫And once that's done, let's hop over to Unreal and play.

85
00:05:43,970 --> 00:05:46,490
‫For some reason I still have this pesky spawn point here.

86
00:05:46,520 --> 00:05:52,700
‫Let's go ahead and delete that and make sure we're spawning near our eye character.

87
00:05:52,970 --> 00:05:53,840
‫Here we go.

88
00:05:53,870 --> 00:05:59,030
‫Let's go and have a look, a little walk around this character, and instantly I can see that he is

89
00:05:59,030 --> 00:06:02,780
‫turning to face my pawn wherever my pawn leads.

90
00:06:02,780 --> 00:06:06,590
‫So that is the beginnings of some AI in place.

91
00:06:06,590 --> 00:06:11,280
‫I know it's not much, but we're going to work on getting this even deeper in the next lecture.

92
00:06:11,300 --> 00:06:12,200
‫I'll see you there.

