How to get widget ref from HUD

BP
I need to get like on screenshot.
My code in HUD

AMyHUD::AMyHUD() : Super() 
{
	ConstructorHelpers::FClassFinder<UUserWidget> WBP_Player(TEXT("/Game/MyGameFolder/HUD/Player/WBP_Player"));
	ConstructorHelpers::FClassFinder<UUserWidget> WBP_WeaponC(TEXT("/Game/MyGameFolder/HUD/Weapon/WBP_WeaponC"));
	
	
	MainHUD = WBP_Player.Class;
	WeaponHUD = WBP_WeaponC.Class;
	
}
void AMyHUD::BeginPlay()
{
	Super::BeginPlay();

	CharacterWidget = CreateWidget(GetWorld(), MainHUD);
	WeaponWidget = CreateWidget(GetWorld(), WeaponHUD);
	
	CharacterWidget->AddToViewport();
	
}

I nned to use weaponwiget ref in weapon script

void AWeapon::SetWeaponWidgetRef(UWeaponWidget* WeaponWidget)
{
	WeaponWidgetRef = WeaponWidget;
}

void AWeapon::UpdateWidgetInfo()
{
	WeaponWidgetRef->UpdateWeaponWidgetInfo(ammoMagazine, currentAmmo,weaponIcon);
}

I want to get ref from getplayercontroller->getHUD
but in my HUD I can’t get ref to UWeaponWidget* how to do it

APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
MyWeapon->SetWeaponWidgetRef(Cast<AMyHUD>(PlayerController->GetHUD())->WeaponWidget);

OR

APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
MyWeapon->SetWeaponWidgetRef(PlayerController->GetHUD<AMyHUD>()->WeaponWidget);

I was able to do this. The problem is that my weapon widget is UUserWidget* since the CreateWidget() function returns in this format, but I need it to be UWeaponWidget*

Error

type argument UUserWidget* is incompatible with type parameter UWeaponWidget*

Then change the type of WeaponWidget to the correct type of UWeaponWidget* (or preferably TObjectPtr< UWeaponWidget >) in your property and then use this:

WeaponWidget = Cast<UWeaponWidget>(CreateWidget(GetWorld(), WeaponHUD));

Thank you

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