I have a PlayerController object and a UserWidget object.
I want to call a Spawn function in PlayerController from UserWidget.
In UserWidget:
AMyPlayerController* playerCtrl = Cast<AMyPlayerController>(UGameplayStatics::GetPlayerController(GetWorld(), 0));
if (playerCtrl) {
UE_LOG(LogTemp, Warning, TEXT("started spawn"));
playerCtrl->Spawn();
}
else
{
UE_LOG(LogTemp, Warning, TEXT("playerController not found"));
}
Playercontroller:
void AMyPlayerController::Spawn()
{
UE_LOG(LogTemp, Warning, TEXT("Start spawn"));
AMyPlayerController* MyController = Cast<AMyPlayerController>(UGameplayStatics::GetPlayerController(this, 0));
AMyPlayer* MyPlayer = Cast<AMyPlayer>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
if (MyController && MyPlayer)
{
FLatentActionInfo LatentInfo;
FName LevelToLoad = "Main";
UGameplayStatics::LoadStreamLevel(GetWorld(), LevelToLoad, true, true, LatentInfo);
MyController->Possess(MyPlayer);
}
UE_LOG(LogTemp, Warning, TEXT("End spawn"));
}
Currently this doesn’t find the GetWorld() in the UserWidget due to it not inheriting the GetWorld() from AActor. How do I reference PlayerController in UserWidget?