The Player Controller

In Unreal Engine, the Player Controller class is used to manage the player's Pawn (a pawn is simply a specialized Actor that can be "possessed" by the player).
Player Controller takes care of handling the input, in our case from the mouse, moving the player ship accordingly, managing the turns, and so on.

As always, the first step is to create a new C++ class, derived from PlayerController.
I'm going to extend it to a Blueprint and call it MyPlayerControllerBP. It's always a good idea to use Blueprints for classes that will handle input, because binding it to custom methods, or giving the player the possibility to customize it, is much easier than having to do it from code.

There's no need to drag the MyPlayerControllerBP icon inside the level to instanciate it, as we did with the Main Map; we just need to set it in the Options.
The first thing to do is to create a custom Game Mode. I did it directly in Blueprint because I'm not going to change the default settings for it, at least for now. So I just clicked on Add New > Blueprint class > Game mode. I called it HexagonsGameModeBP. The next step is to click on Settings > Project Settings > Maps & Modes and assign our new class to Default Game Mode. 


This will enable all the options for the Selected Game Mode, allowing us to change the Player Controller class to our custom MyPlayerControllerBP.

Moving to Visual Studio, here's how I edited the MyPlayerController.h file:


I added 2 UPROPERTY variables that that will store a reference to 2 different materials I created for the Hexagon mesh: one is the standard, white material and the other is the "hilight" material (I chose a neon green) that will show up when the mouse is hovering on a tile.
If we move to the Blueprint editor for the MyPlayerControllerBP class after compiling the project (always remember to do this before switching to Unreal Editor, to let it know your latest changes), that's how they look:



As you can see from the code snippet, there are also 2 UFUNCTION methods. This makes them callable from the Blueprint, and I'll show you how this is used with the MouseClicked() method:

In the Graph editor of the MyPlayerControllerBP Blueprint we can press the spacebar to create a new Event and search for the "Left Mouse Button" (just type the first chars, it will show up in the drop-down list). Let's add it. Now you can choose what happens when we press or release the button. I'll drag a line from the "pressed" pin and type the name of our function "MouseClicked" in the searchbar. 



The method is visible here because we declared it "Blueprint Callable" in the C++ header file, and now everytime a user clicks the mouse button, our method will be triggered. You can check if it's working by simply putting a UELOG statement inside the method, something like this:

UE_LOG(LogTemp, Warning, TEXT("Mouse clicked."));

Compile, launch the game and this message should appear in the log everytime you click.

Next time we'll try to manage the "mouse hover", changing the highlighted hexagon as the mouse moves on it.

Game you next time! 


Commenti