Good day! There was a problem with calling a function from the Base_HUD class, in the WidgetHUD widget class. I wanted to implement the following functionality: when you click on a button in the widget, the function to show the pause menu (ABase_HUD :: ShowPauseMenu ()) was called, but when directly accessing it, an error occurred that such use is not allowed, a static function is required, declaring the function as static void ShowPauseMenu (), additional errors are thrown. I do not completely understand whether I chose the correct path to call this function, so tell me how to fix this problem, or point to the correct path to call this function. Thanks in advance
WidgetHUD.h
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "WidgetHUD.generated.h"
UCLASS()
class ARCADE_API UWidgetHUD : public UUserWidget
{
GENERATED_BODY()
virtual bool Initialize();
UPROPERTY(meta = (BindWidget))
class UButton* PauseGame;
UFUNCTION()
void ClickedPauseGame();
UPROPERTY(meta = (BindWidget))
class UButton* Skill_1;
UPROPERTY(meta = (BindWidget))
class UButton* Skill_2;
UPROPERTY(meta = (BindWidget))
class UButton* Skill_3;
};
WidgetHUD.cpp
#include "HUD/Widget/WidgetHUD.h"
#include "Components/Button.h"
#include "HUD/Base_HUD.h"
bool UWidgetHUD::Initialize()
{
Super::Initialize();
PauseGame->OnClicked.AddDynamic(this, &UWidgetHUD::ClickedPauseGame);
return true;
}
void UWidgetHUD::ClickedPauseGame()
{
ABase_HUD::ShowPauseMenu();
}
Base_HUD.cpp
#include "HUD/Base_HUD.h"
#include "UObject/ConstructorHelpers.h"
#include "HUD/Widget/WidgetHUD.h"
#include "HUD/Widget/WidgetPauseMenu.h"
#include "HUD/Widget/WidgetMainMenu.h"
#include "GameFramework/Pawn.h"
ABase_HUD::ABase_HUD()
{
ConstructorHelpers::FClassFinder<UWidgetHUD> DefaultHUD(TEXT("WidgetBlueprint'/Game/Blueprint/HUD/BP_PlayerHUD'"));
HUDClass = DefaultHUD.Class;
ConstructorHelpers::FClassFinder<UWidgetMainMenu> DefaultMainMenu(TEXT("WidgetBlueprint'/Game/Blueprint/HUD/BP_MainMenu'"));
MainMenuClass = DefaultMainMenu.Class;
ConstructorHelpers::FClassFinder<UWidgetPauseMenu> DefaultPauseMenu(TEXT("WidgetBlueprint'/Game/Blueprint/HUD/BP_PauseMenu'"));
PauseMenuClass = DefaultPauseMenu.Class;
}
void ABase_HUD::BeginPlay()
{
Super::BeginPlay();
PController = ()->GetFirstPlayerController();
ABase_HUD::ShowPlayerHUD();
}
void ABase_HUD::RemoveFromParent(class UUserWidget*)
{
}
//Функция для создания виджета
void ABase_HUD::CustomCreateWidget(TSubclassOf<class UUserWidget> WClass, class UUserWidget *WRef)
{
if (WClass != NULL)
{
WRef = CreateWidget<UUserWidget>((), WClass);
if (WRef != NULL)
{
WRef->AddToViewport();
}
}
}
void ABase_HUD::ShowPauseMenu()
{
ABase_HUD::RemoveFromParent(WidgetHUD);
PController->SetInputMode(FInputModeUIOnly());
UE_LOG(LogTemp, Log, TEXT("PAUSED"));
UGameplayStatics::SetGamePaused((), true);
ABase_HUD::CustomCreateWidget(PauseMenuClass, WidgetPauseMenu);
}
void ABase_HUD::ShowPlayerHUD()
{
UGameplayStatics::SetGamePaused((), false);
ABase_HUD::RemoveFromParent(WidgetMainMenu);
ABase_HUD::RemoveFromParent(WidgetPauseMenu);
PController->SetInputMode(FInputModeGameOnly());
ABase_HUD::CustomCreateWidget(HUDClass, WidgetHUD);
}
void ABase_HUD::ShowMainMenu()
{
ABase_HUD::RemoveFromParent(WidgetPauseMenu);
PController->SetInputMode(FInputModeUIOnly());
UGameplayStatics::SetGamePaused((), true);
ABase_HUD::CustomCreateWidget(MainMenuClass, WidgetMainMenu);
}
Base_HUD.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/HUD.h"
#include "Blueprint/UserWidget.h"
#include "Kismet/GameplayStatics.h"
#include "Base_HUD.generated.h"
UCLASS()
class ARCADE_API ABase_HUD : public AHUD
{
GENERATED_BODY()
public:
ABase_HUD();
APlayerController* PController;
protected:
virtual void BeginPlay() override;
virtual void RemoveFromParent(class UUserWidget*);
virtual void CustomCreateWidget(TSubclassOf<class UUserWidget> WClass, class UUserWidget *WRef);
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UI", Meta= (BlueprintProtected= "true"))
TSubclassOf<class UUserWidget> HUDClass;
UPROPERTY()
class UUserWidget* WidgetHUD;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UI", Meta = (BlueprintProtected = "true"))
TSubclassOf<class UUserWidget> MainMenuClass;
UPROPERTY()
class UUserWidget* WidgetMainMenu;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UI", Meta = (BlueprintProtected = "true"))
TSubclassOf<class UUserWidget> PauseMenuClass;
UPROPERTY()
class UUserWidget* WidgetPauseMenu;
public:
static void ShowPauseMenu();
static void ShowPlayerHUD();
static void ShowMainMenu();
};