How can the Actor get the HUD class that I create ?
I tried this didn’t work:
AGameMode * MyGameMode = Cast(UGameplayStatics::GetGameMode(this));
AMyHUD * hud = Cast(MyGameMode->HUDClass.GetDefaultObject());
hud->Player1Score++;
How can the Actor get the HUD class that I create ?
I tried this didn’t work:
AGameMode * MyGameMode = Cast(UGameplayStatics::GetGameMode(this));
AMyHUD * hud = Cast(MyGameMode->HUDClass.GetDefaultObject());
hud->Player1Score++;
AMyHUD * hud = Cast(UGameplayStatics::GetPlayerController(this, 0)->GetHUD());
or AMyHUD * hud = Cast(GetWorld()->GetFirstPlayerController()->GetHUD());
I’ve got the same problem and it doesn’t work for me, because the compiler doesnt’t know the AMyHUD class / blueprint. So how do I include it?
Create a class called AMyHUD first. AHUD is the base HUD class in unreal so AMyHUD doesn’t exist anywhere until you actually create the class.
You still need to put the casting type of course.
Cast<AMyHUD>(GetWorld()->GetFirstPlayerController()->GetHUD());
These answers helped me so much, thanks!
There is a templated version of the GetHUD() method to cast directly to your custom HUD class:
AMyHUD * hud = UGameplayStatics::GetPlayerController(this, 0)->GetHUD<AMyHUD>();
Cast(UGameplayStatics::GetPlayerController(this->GetOwner(), 0)->GetHUD())