When should I create the Widget??

Hello!

Im trying to create a multiplayer game, I have the Spawn logic in the GameMode, so the server spawns the Player Actor and then the controller possess it.

The problem is that I have the logic of creating the widget and adding to the viewport in the Pawn, because I want to switch pawns while gameplay.

So, when the GameMode does the part ThisController->Possess(Pawn), this goes to the PossessedBy and there I trigget a Client RPC to create the UI.
But GetOwningPlayerPawn() in the UI Widget returns null.

To be more clear, if I do this:

void UCMainCharacterWidget::NativeOnInitialized()
{
	Super::NativeOnInitialized();

	if (!GetOwningPlayer<ACMyPlayerController>())
	{
		UE_LOG(LogTemp, Error, TEXT("Get Owning Player not found"));
		UKismetSystemLibrary::PrintString(GetWorld(), TEXT("Get Owning Player not found"), true, true, FLinearColor(0.66,0,0), 10);
		if (!GetOwningPlayer()->GetPawn<ACMyCharacter>())
		{
			UE_LOG(LogTemp, Error, TEXT("Get Owning Pawn not found"));
			UKismetSystemLibrary::PrintString(GetWorld(), TEXT("Get Owning Pawn not found"), true, true, FLinearColor(0.66,0,0), 10);
		}
	}

	if (ACMyPlayerController* MyPlayerController = GetOwningPlayer<ACMyPlayerController>())
	{
		UE_LOG(LogTemp, Error, TEXT("BREAKPOINT 2"));
		if (ACMyCharacter* MyPlayerCharacter = MyPlayerController->GetPawn<ACMyCharacter>())
		{
			UE_LOG(LogTemp, Error, TEXT("BREAKPOINT 3"));
			MyPlayerCharacter->CPPlayerStats->OnStatChanged.AddDynamic(this, &UCMainCharacterWidget::UpdateHealth);
		}
	}
}

I dont get the “Get Owning XXXX not found”, so It has a correct ACMyPlayerController and ACMyCharacter.
I Get “BREAKPOINT 2” but not “BREAKPOINT3”.

YMMV, but we spawn all widgets in the GameMode’s designated AHUD::BeginPlay() class / function.

Then if your objects need a HUD wiget then they can find it.

If you wanted to get more fancy, make an interface to your HUD and nothing would ever directly reference a Widget, just the interface.

But if you spawn it on the AHUD::BeginPlay, when you do the CreateWidget(Owner, ClassOfTheWidget). In the owner you put the player controller or GetWorld()?

Owner is GetWorld()

Hey @Shmoopy1701 I am really thank you for your answers.

I got a new solution for the possessing thing.
I was making a client RPC OnPossess and OnUnpossess but I found that Pawn has a overridable method that is (virtual void NotifyRestarted() override;)

This method executes when pawn is possess in client after the client has possess it…

I have been looking for that method for a long time…

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.