‫Hello and welcome.
‫In this lecture we're going to be working on AI Focus.
‫So that is some built in functionality in Unreal that we can use to make sure that our AI characters
‫follow us around the world are able to aim at us no matter where we go.
‫Let's dive in and see how focus works.
‫So I currently doesn't really do very much.
‫And I think a first starting point for it to do anything would be for it to turn and face towards our
‫player.
‫Now to do this, we can use some built in functionality of the AI, which is setting a focal point.
‫So if you have a look at the AI control and look for set focus, there's a couple of relevant methods
‫here.
‫One is set focal points, the other is set focus, and the one just below that is clear focus.
‫So the idea here is that you can tell the AI controller that it should be trying to focus either on
‫a particular point or it should be trying to focus on a particular actor.
‫If you choose the latter, if you choose to focus on an actor, then what's going to happen is as that
‫actor moves, you're going to keep focusing on that actor.
‫Whereas if you chose to focus on a particular point, you would keep focusing on that point.
‫But if that point was the location of an actor, when that actor moves, that point does not get updated.
‫So typically focal points are for things that are fixed in the world, actors for things that move around
‫that kind of makes sense.
‫And then we have this clear focus function down here, which tells us that we should stop focusing on
‫something.
‫So that's how it works.
‫You set a focus, you clear a focus.
‫Now there is this priority variable in here which we are going to come to a little bit.
‫But the idea here is that you've got different levels of priority.
‫We've got this enum here which by default on set focus and set focal point is set to gameplay.
‫If we go and have a look at the definition of that enum, you can see there is a default move and gameplay
‫and they're basically listed in the order of priority.
‫So gameplay will have the highest priority.
‫If you set something as gameplay, that's what it's going to look at.
‫If gameplay isn't set because you cleared the gameplay priority, then it's going to look where it's
‫going and if it's not moving anywhere, then it's going to just look at the default priority, which
‫might just be somewhere off in the distance.
‫So if you've got a guard that's got a patrolling behavior, he'll default to looking to where he's next
‫moving to once he gets there.
‫You could set a default point for him to look at.
‫So, I don't know, maybe he's looking at the entry way to his particular zone or something like that.
‫Anyway, this is what the priorities are all about.
‫As I say, we won't have to be using them too much because we're mostly going to be using this gameplay
‫functionality to say that, Hey, there's something interesting going on over there.
‫There's the player, so let's go ahead and look at him.
‫So the first thing we're going to do is we're going to implement a begin play on our AI controller so
‫that we can set the focus on Begin Play.
‫This is just going to be to show us how this function works.
‫So Begin Play, as we've probably seen before, it's a protected function.
‫So we need to create a protected section and then we want our virtual void begin play.
‫We're going to put the override keyword just for safety here and then we're going to go ahead and create
‫the implementation over in the C++.
‫And we're going to want to call to super begin play because that's always good practice because otherwise
‫things in blueprints go wrong.
‫If you forget to do this, if you ever have a problem with blueprints after you've created a begin play,
‫make sure you check that you're calling to super.
‫And then at this point, what we want to do is get hold of the pawn actor and set the focus to it.
‫To get hold of a pawn actor, we can use a function from the gameplay statics header which if you hover
‫over this or search in control p to find gameplay statics, you can find that this is under the kismet
‫forward slash gameplay statics.
‫So we'll go back over to the shooter I controller CP and include that.
‫So hash include and then it's going to be kismet forward slash gameplay statics and then we should be
‫able to do our U gameplay statics and use a function from within here which I find very useful is the
‫get player pawn, which is going to return as a pointer to a player pawn.
‫And it does that by taking in a world context object and the player index.
‫In our case we're going to find the zero player.
‫So let's get play a pawn and do a get world.
‫And then we're going to look for index zero and we're going to save this into a variable, a pawn.
‫At which is going to be the player -- like.
‫So that's what we want to focus on.
‫So it's challenge time for you to set the focus.
‫Use the player -- that we've just got using our gameplay statics function call, set, focus and test
‫it out in the world to see if you move around our different characters.
‫Will they follow you?
‫Will their gaze follow you?
‫Pause video and find out.
‫Okay.
‫Welcome back.
‫So hopefully it wasn't too difficult.
‫We need to call set focus because set focus is on a parent.
‫We don't have to find any pointers here.
‫We do set focus and what have we got here?
‫We just need to put in the actor basically because the priority is already got a default of being set
‫to the gameplay, which is what we wanted anyway, so we can just pass in our player pawn and that should
‫be who we focus on.
‫So let's go ahead and build.
‫And once that's done, let's hop over to Unreal and play.
‫For some reason I still have this pesky spawn point here.
‫Let's go ahead and delete that and make sure we're spawning near our eye character.
‫Here we go.
‫Let's go and have a look, a little walk around this character, and instantly I can see that he is
‫turning to face my pawn wherever my pawn leads.
‫So that is the beginnings of some AI in place.
‫I know it's not much, but we're going to work on getting this even deeper in the next lecture.
‫I'll see you there.