CreateWidget cannot be used on Player Controller with no attached player

I’m calling UGameplayStatics::CreatePlayer to create a second local player. I can confirm from the World Outliner that a second player controller is created, together with all supporting classes (PlayerState, Pawn, etc.).

At the Controller BeginPlay, in the Blueprint, I’m calling CreateUserWidget to create the UI for the player. At that point I get an error: “CreateWidget cannot be used on Player Controller with no attached player”.

Shouldn’t this be done by default? I’ve, alternatively, called CreatePlayer from the GameMode blueprint, but the result is the same.

*I’ve already asked this question at *CreateWidget cannot be used on Player Controller with no attached player - C++ - Unreal Engine Forums.

I’m looking at the source from github, and I’m perplexed. The error seems to come from here:

https://github.com/EpicGames/UnrealEngine/blob/2bf1a5b83a7076a0fd275887b373f8ec9e99d431/Engine/Source/Runtime/UMG/Private/UserWidget.cpp#L1902



else if (!OwnerPC.Player)
{
     const FText FormatPattern = LOCTEXT("NoPlayer", "CreateWidget cannot be used on Player Controller with no attached player. {PlayerController} has no Player attached.");
     FFormatNamedArguments FormatPatternArgs;
     FormatPatternArgs.Add(TEXT("PlayerController"), FText::FromName(OwnerPC.GetFName()));
     FMessageLog("PIE").Error(FText::Format(FormatPattern, FormatPatternArgs));
}


Now, this means that the PlayerController doesn’t have a Player. But, I’m creating a PC through GameStatics:

https://github.com/EpicGames/UnrealEngine/blob/2bf1a5b83a7076a0fd275887b373f8ec9e99d431/Engine/Source/Runtime/Engine/Private/GameplayStatics.cpp#L264


APlayerController* UGameplayStatics::CreatePlayer(const UObject* WorldContextObject, int32 ControllerId, bool bSpawnPlayerController)
{
UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
FString Error;

ULocalPlayer* LocalPlayer = World ? World->GetGameInstance()->CreateLocalPlayer(ControllerId, Error, bSpawnPlayerController) : nullptr;

if (Error.Len() > 0)
{
UE_LOG(LogPlayerManagement, Error, TEXT("Failed to Create Player: %s"), *Error);
}

return (LocalPlayer ? LocalPlayer->PlayerController : nullptr);
}

Which should have a player attached.

One final note: I’m checking for a player right after calling CreatePlayer:



const auto Player = GetWorld()->GetGameInstance()->CreateLocalPlayer(-1, Error, true);

check(Player);
check(Player->PlayerController);