How to possess a pawn in its constructor?

I have a pawn created in C++ and I want it to be possessed by default. If I set Autopossess to Player 0 in the editor it works fine, but I want to do it directly in code. But I can’t figure out how to get the reference of the pawn from within its own constructor. I’ve tried this:

Controller->Possess(this);

And that builds without problem but the editor crashes when I’m trying to start the program. I’ve tried moving it to the BeginPlay() function as well, that then it starts, but nothing happens.

What should “this” be replaced with? How do I get a reference to the pawn itself from within its own constructor? Or, it’s asking for “APawn *InPawn”, not sure what that is though…?

if a pawn does not have a controller yet, what controller are you trying to get a reference to?

you should set the default pawn from the game mode.

possession is a one way relationship between a controller and pawn, and both are spawned by the game mode.

a pawn cannot tell its controller to possess it, because possession is where pawns get a reference to its controller in the first place. and that relationship is 1 way, meaning it is playerControllers that possess or unpossess pawns, and pawns have no say in the matter.

both playercontroller and pawn are spawned from the gameMode, and possession happens during postLogin, in the GameMode::RestartPlayer function, based on DefaultPawnClass and PlayerControllerClass.

Hmm… I have to read up on how to make a custom PlayerController. I hope that will help. Thanks for the tips! I get back when I have looked into that.

Where is the PlayerController for the vehicle template in Unreal? I cant wrap my head around this, I need some example code to look at.

any player controller should be able to work with any pawn, as long as they are designed correctly. if you make a playercontroller know anything about the specific kind of pawn its working with, you won’t be able to posses a different kind of pawn, which will limit your flexibility in the kinds of games you can make.

a player controller should know its controlling a pawn, but it should not know what kind of pawn its controlling.

a pawn should know it has a controller, but a pawn should not assume its a playerController, because it might be an AI controller, or some other kind of controller.