Hi there. I encountered this problem with the crosshair and the health bar still appeared in the start menu unintended. I intended to make the crosshair and the health bar only appeared when we actually playing the game. I also went far to make separate level for main menu purpose only but still this problem happened persistently.
Here is my ShooterPlayerController cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "ShooterPlayerController.h"
#include "TimerManager.h"
#include "Blueprint/UserWidget.h"
void AShooterPlayerController::BeginPlay()
{
Super::BeginPlay();
HUD = CreateWidget(this, HUDClass);
if (HUD != nullptr)
{
HUD->AddToViewport();
}
}
void AShooterPlayerController::GameHasEnded(class AActor* EndGameFocus, bool bIsWinner)
{
Super::GameHasEnded(EndGameFocus, bIsWinner);
HUD->RemoveFromViewport();
if (bIsWinner)
{
UUserWidget* WinScreen = CreateWidget(this, WinScreenClass);
if (WinScreen != nullptr)
{
WinScreen->AddToViewport();
}
}
else
{
UUserWidget* LoseScreen = CreateWidget(this, LoseScreenClass);
if (LoseScreen != nullptr)
{
LoseScreen->AddToViewport();
}
}
GetWorldTimerManager().SetTimer(RestartTimer, this, &APlayerController::RestartLevel, RestartDelay);
}
Here is my ShooterPlayerController.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "ShooterPlayerController.generated.h"
/**
*
*/
UCLASS()
class SIMPLESHOOTER_API AShooterPlayerController : public APlayerController
{
GENERATED_BODY()
public:
virtual void GameHasEnded(class AActor* EndGameFocus = nullptr, bool bIsWinner = false) override;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
UPROPERTY(EditAnywhere)
TSubclassOf<class UUserWidget> HUDClass;
UPROPERTY(EditAnywhere)
TSubclassOf<class UUserWidget> LoseScreenClass;
UPROPERTY(EditAnywhere)
TSubclassOf<class UUserWidget> WinScreenClass;
UPROPERTY(EditAnywhere)
float RestartDelay = 5;
FTimerHandle RestartTimer;
UUserWidget *HUD;
};
Any help will hugely appreciated. Cheers.