C++ Pawn Possession

Is there a way to possess a pawn like this

but in c++ code, because so far when i type “unreal engine 4 c++ possession” all that comes up is blueprints

Here is the particular function in the docs: AController::Possess | Unreal Engine Documentation

This function occurs on the controller, so first you’ll want to get the particular character controller then call this function on it with whatever pawn you are switching to (it will unpossess automatically).

Possesing a new pawn would require the following steps:

  • Cache a reference to your current pawn
  • Unposses your current pawn (Controller->UnPossess())
  • Spawn the new pawn using a different class
  • Posses the new pawn (Controller->Possess(NewPawn))
  • If all went well destroy the cached pawn, if not you could always re-posses the old one.

Cheers,
Moss

2 Likes

You should not forget to cache your player controller too

APlayerController* controller = GetController();
controller->UnPossess();
ACharacter* character = GetWorld()->SpawnActor...
controller->Possess(character);

otherwise GetController() from unpossessed pawn will return null

1 Like

what includes please ?