I have been using UserWidgets
in C++ to do the UI in a game I’m working on. I’m trying to open a settings window from the main menu. None of my UserWidgets
have been causing problems like this before. I have a In/Out animation I show when a UserWidgets
is added/removed from the parent. I was trying to wait until the animation was done before removing it from the viewport, was working fine in Blueprints before moving it to C++. If I do it in OnAnimationFinished_Implementation
it causes the settings widget to take 10-15 seconds to show. The code above it showing the buttons has no issues.
void UMainMenuWidget::OnAnimationFinished_Implementation(const UWidgetAnimation* Animation)
{
Super::OnAnimationFinished_Implementation(Animation);
FString AnimationName = Animation->GetDisplayName().ToString();
if (AnimationName == OpenAnimationName)
{
PlayButton->SetIsEnabled(true);
SettingsButton->SetIsEnabled(true);
QuitButton->SetIsEnabled(true);
}
if (AnimationName == OutAnimationName)
{
if (const UZombieHordeGameInstance* MainGameInstance = GetGameInstance())
{
MainGameInstance->HideWidget(EWidgetNames::MainMenu);
MainGameInstance->ShowWidget(EWidgetNames::Settings);
}
else
{
UE_LOG(LogMainMenuWidget, Error, TEXT("OnAnimationFinished_Implementation: GameInstance not initialized"))
}
}
}
If I comment out the above 2 lines and do the below in the click handler then the settings widget opens immediately as expected.
void UMainMenuWidget::OnSettingsClicked()
{
PlayAnimation(Out);
//GetGameInstance()->HideWidget(EWidgetNames::MainMenu);
//GetGameInstance()->ShowWidget(EWidgetNames::Settings);
}