In this lecture, you'll learn how to use the mouse location in your Unreal games. So let's go take a look. In this lecture we'll create two new scripts and we'll use the mouse input game mode base script that Unreal automatically provided to us when we created this project. Because we're going to write a custom game mode like we've done before. The other two scripts I've created here are a character pawn and a character player controller. So as I mentioned, the big idea is the pawn is the avatar for the player in the game. And the player controller will possess the pawn to make it bend to its will, if you will. So we've got the pawn to represent the player in the game. And then the player controller will control how the pawn behaves. Before we look at those three scripts, I'm going to navigate to the blueprints that I have in the game. So I have turned the character pawn script into a blueprint. And I gave it a static mesh representation so we can see it in the game. And I turned the character player controller into a blueprint, that as well. So I've converted those two scripts to blueprint. We'll look at our custom game mode base for this project first. So I've added a constructor here. This doesn't come for free when we have the game mode base as the default that gets created by Unreal. So we need to add a constructor for it. And here's where I do some serious work. So what we need to do here in our custom game mode, is we need to set up what the default pawn is in the game and what the player controller is that controls that pawn. Because I have these as blueprints, I'm going to use this FObjectFinder class, that's a templated class. So we say, what kind of object are we looking for? And blueprint classes are of the data type UClass. And then I tell it what I want to name this variable. And then I have this really interesting stuff here, that says, where can I find that blueprint class definition? Back in the editor, the way we get a location of our blueprint is we right click on the blueprint and we say Copy Reference. Now I'm going to copy that reference into the code, even though it's not quite correct for what we need, and I'll show you how we can change it. So I'll just find a blank space here to put it. So if I Ctrl+V now, I actually have a reference to the blueprint itself, not the blueprint class. And I want to reference to the blueprint class. So I take what I got here, And I put it between the double quotes here. And the two changes I make are, I changed Blueprint to Class, and I put _C at the end. Because that's what Unreal does to name the blueprint class that goes along with this blueprint. So I want to load the class here, so that's what I'm doing. Next, The FObjectFinder class has an Object property that will tell us the object for the asset that was loaded. And I'm checking here to make sure that's not a null pointer. If trying to load this pawn class fails, then PawnClass.Object will be null and I better not do anything with it. So this is my safety check to make sure that worked properly. And if it's not null, finally, I can set the default pawn class, the class of the default pawn in my game to PawnClass.Object. I do a very similar thing for the player controller. I turned it into a blueprint, so I use this line of code to go find the class associated with that blueprint. I make sure that I didn't get a null pointer. And as long as I didn't get a null pointer, I set the player controller class to be ControllerClass.Object. So by doing this in my custom game mode, what will happen is, when the game starts, Unreal will automatically possess the default pawn class with the player controller class. It will also place that default pawn at player start, which is right here. I've set it to 0,0,0, so it will put it right in the middle of the screen for me. Remember, if we change the game mode, we need to go to Edit, Project Settings, Maps and Modes. And here we need to set our default game mode. And we're using the custom game mode that I was just working on. And that's an important step. If you forget to do that and you did all that work I just did, if you forget to do that, then when you run your game you'll just end up with a sphere in the center and you'll wonder why your characters aren't showing up there. And the reason is, the most common reason is, that you didn't remember to change the game mode. Okay, let's see how the pawn class works next. So here we are in the character pawn header file. And these are default things we get, we get a constructor, we get a BeginPlay function, we get a Tick function. We get a SetupPlayerInputComponent function, because sometimes you can set it up so the pawn handles all its own input processing, it's not possessed by a player controller. I'm going to consistently throughout the specialization just use a player controller to possess the pawn. Or a different kind of controller, if I set it up so that it has artificial intelligence, it won't be a player controller, it'll be an AI controller, but something will possess all my pawns. So we get this function by default, but I'm not going to use it. I did however, add a function that will let a consumer of this class call the SetLocation function, so that I can move the pawn to a different location. The way I move a pawn to a different location is I call this function, SetActorLocation and I do it with a location. And because a pawn is a special kind of actor, it just moves the pawn to the provided location. The most interesting stuff is in our character player controller script. Here in the header file, I've added a PlayerTick function. And I need that because I'm going to make the pawn follow the mouse location on every frame. And so I need to tick every frame to do that. It's called PlayerTick here in a player controller, rather than Tick like we've seen in pawns and actors, but it does the same thing. So in PlayerTick, I do a number of different things. The tricky part is the mouse is in screen coordinates. It's just this flat plane, right, that we move the mouse along, but it's not in the game world, it's just on our screen. To get a world location, so we can actually make our character follow the mouse, we need to convert the screen location that the mouse is on into the world location where we want the character. The way we do that is we use the DeprojectMousePositionToWorld function that appears in the player controller class. And here's the documentation for that. It says, it converts the current mouse 2D position to a world space 3D position and direction. We don't really care about the direction here in our 2D games, but in a 3D game, you would care about the direction as well. So we provide an FVector for the world location and an FVector for the world direction when we call this DeprojectMousePositionToWorld function. And that's why I declared these two variables before I call the function. So now, after I call the function, when I get to line 23 here, WorldLocation is the location in the 3D world that corresponds to the location of the mouse in the 2D space of the screen. Now we want to move our character to that location. But before we do, I want to set WorldLocation.X to 0. Remember, we're keeping everything on the YZ plane. So I'm going to move the pawn to stay in the YZ plane. How do I actually get the pawn? Well, I call the GetPawn function, and that gets the pawn that is possessed by this player controller. I will say that that actually returns an APawn pointer. And because I need to call a function on an ACharacterPawn, I need to typecast the results of that function call to be in ACharacterPawn pointer. And that works, because an ACharacterPawn is a special kind of APawn, because I picked Pawn as the parent when I created this script. And I put that into a variable called CharacterPawn. Again, for safety, I make sure that that GetPawn function worked fine by making sure CharacterPawn is not a null pointer. And if it's not, I can now say on that CharacterPawn pointer, call the function. Remember this arrow notation is what we use if we need to call a function on a pointer to an object rather than the object itself. And then I call that SetLocation function that I showed you earlier. And I provide the WorldLocation that I extracted from the mouse and set to the YZ plane. So back in the editor, I can now run my game, and I have this character. So Peak Game Studios actually built a number of games for the Pikes Peak Library District to use to teach fourth graders about Colorado history. And that was a 2D Flash game, but my 3D character modeler turned those characters into 3D models that we can use here. So this is one of the characters from that game. And as you can see, she follows the mouse around as I move the mouse around. Unfortunately, if I drag the mouse outside of the screen, she leaves the screen as well, including top and bottom as well, of course. And there may be some games where we want to actually keep the player in the screen, and we'll address that particular issue in the next lecture. So this is all the code and all the setup to make our character follow the mouse around the screen. To recap, in this lecture, you learned how to retrieve and use the mouse location in your Unreal games.