Last active
January 24, 2022 12:37
-
-
Save MichaelGatesDev/2b665df030516cb0cd302f6b06eec5dd to your computer and use it in GitHub Desktop.
Example of creating a local version of OnPossess for Unreal Engine 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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