Skip to content

Instantly share code, notes, and snippets.

@MichaelGatesDev
Last active January 24, 2022 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelGatesDev/2b665df030516cb0cd302f6b06eec5dd to your computer and use it in GitHub Desktop.
Save MichaelGatesDev/2b665df030516cb0cd302f6b06eec5dd to your computer and use it in GitHub Desktop.
Example of creating a local version of OnPossess for Unreal Engine 4
// in game instance header file
virtual void Init() override;
UFUNCTION()
void DispatchPawnControllerChange(APawn* InPawn, AController* InController);
// in game instance cpp file
void UVGameInstanceBase::Init()
{
Super::Init();
TScriptDelegate<FWeakObjectPtr> OnPawnControllerChangeDelegate;
OnPawnControllerChangeDelegate.BindUFunction(this, "DispatchPawnControllerChange");
OnPawnControllerChangedDelegates.Add(OnPawnControllerChangeDelegate);
}
void UVGameInstanceBase::DispatchPawnControllerChange(APawn* InPawn, AController* InController)
{
if (!InController)
{
UE_LOG(LogTemp, Display, TEXT("Pawn %s was un-possessed"), *InPawn->GetName());
return;
}
if (!InController->IsPlayerController())
{
UE_LOG(LogTemp, Display, TEXT("Pawn %s was possessed by non-player controller %s"), *InPawn->GetName(), *InController->GetName());
return;
}
MyPlayerController* PlayerController = Cast<MyPlayerController>(InController);
if (!PlayerController)
{
UE_LOG(LogTemp, Display, TEXT("Pawn %s was possessed by player controller, but not MyPlayerController"), *InPawn->GetName());
return;
}
PlayerController->OnPossessLocal(InPawn); // OnPossessLocal would be a function declared in your custom PlayerController class
UE_LOG(LogTemp, Display, TEXT("Pawn %s was possessed by MyPlayerController"), *InPawn->GetName());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment