Associating a pawn with a player controller

Hello, I was curious on how to go about creating or associating a pawn with a corresponding player controller. Should I create a “pawn” instance within the constructor of the PlayerController or should I create two separate classes, one being the PlayerController and the other being the pawn. According to the documentation on PlayerController, You definitely want some functionality in the PlayerController as opposed to the pawn, such as in a death match scenario.

I was wondering how to go about setting this up, for I want to be able to give commands to the Pawn through the player controller, and save data such as “score” in the PlayerContoller as opposed to the pawn, for I want that information to save.

Any direction would be much appreciated, Thanks!

Hello LostScout,

Whenever you are controlling a character or pawn at runtime, you can think of the character or pawn as a puppet and the player controller as the strings. Pawns that are being controlled are possessed by a PlayerController. In this sense, the PlayerController is linked to the pawn. You can easily store any information you like in the PlayerController and base events off the values listed there rather than looking at the Pawn’s values. An example would be destroying the possessed Pawn when the PlayerController’s health value reaches 0. It can be then be reset to max after 5 seconds, respawn the Pawn, and possess the Pawn.

Hope this helps!

I this case, would I be calling the possess function in the playercontroller class like… [ this->possess(mypawn) ]
or would a create in instance within mypawn and create a pointer object
MyPlayerController and then do [ MyPlayerController->possess(mypawn) ] ?

The answer to this question will allow me to visualize the connection

You would use possess(mypawn) in the PlayerController’s class as the second method you mentioned would be creating a new PlayerController instance which wouldn’t have any of the values that you had set up to that point, which could cause some odd behavior and loss of data. A PlayerController is automatically created for you as the start of play if you set your Default PlayerController Class to match your custom class.

would this mean that in my PlayerController`s constructor I would do
MyPlayerController = DefaultPlayerController;
or would you have to use the GetPlayerController() function somehow?

  • thanks in advanced

When you set up your game mode, there is a field for your Default PlayerController which can be set to your specific PlayerController. Once this is done, your PlayerController class will be the default, and an instance of that class will be spawned when the session begins.

so, how would I reference that instance? In terms of some code, would I use the this-> keyword? Once I have set MyPlayerController as the default playercontroller within the gamemode, how would I reference and call the possess function in MyPlayerController`s constructor? How do I specify that instance of MyPlayerController to possess a pawn(code example would help).

You would reference the same way that you would in Blueprints, which means using the GetPlayerController function which is located in the UGameplayStatics function library.

To include UGameplayStatics, add this to your includes:

#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"

To use the GetPlayerController function, you’d use this syntax:

ACustomPlayerController* MyController = UGameplayStatics::GetPlayerController(this, 0);

If you expect that there may be different types of PlayerControllers, you could cast instead to be safe:

ACustomPlayerController* MyController = Cast<ACustomPlayerController>(UGameplayStatics::GetPlayerController(this, 0));

    *logic here*

If the cast fails, MyController will be left with a null value which will cause the If statement to fail as well, avoiding any mistakes in the case of getting a playercontroller of a different class.

Thank you!

I am wanting to do a closely related thing, except I am still waiting for the C++ compiling error to be fixed. I have a number of pawn classes(Currently just 2) and I was using Blueprint Widget “Posses” to change classes.