Created
June 12, 2024 18:42
-
-
Save rtm223/99ef8641d7b8882adfa98077554ffc58 to your computer and use it in GitHub Desktop.
Base Class Unreal HUD that can hide all widgets via the ShowHUD console command and when you enter simulate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "UI/HUD/RTMHUDBase.h" | |
#include "Engine/LocalPlayer.h" | |
#include "GameFramework/PlayerController.h" | |
#include "Widgets/SViewport.h" | |
namespace RTMHUDBaseStatics | |
{ | |
SWidget* FindWidgetByTypeRecursive(SWidget* root, FName widgetType) | |
{ | |
SWidget* ret = nullptr; | |
root->GetChildren()->ForEachWidget([widgetType, &ret](SWidget& child) | |
{ | |
if(!ret) | |
{ | |
if(child.GetType() == widgetType) | |
ret = &child; | |
else | |
ret = FindWidgetByTypeRecursive(&child, widgetType); | |
} | |
}); | |
return ret; | |
} | |
} | |
void ARTMHUDBase::BeginPlay() | |
{ | |
Super::BeginPlay(); | |
#if WITH_EDITOR | |
FEditorDelegates::OnSwitchBeginPIEAndSIE.AddUObject(this, &ThisClass::HandleSwitchBetweenPieAndSIE); | |
#endif | |
UpdateUIVisibility(); // Respect bShowHUD in the details panel | |
} | |
void ARTMHUDBase::ShowHUD() | |
{ | |
Super::ShowHUD(); | |
UpdateUIVisibility(); | |
} | |
#if WITH_EDITOR | |
bool ARTMHUDBase::IsSimulatingInEditor() | |
{ | |
const auto* editorEngine = Cast<UEditorEngine>(GEngine); | |
return editorEngine && editorEngine->bIsSimulatingInEditor; | |
} | |
void ARTMHUDBase::HandleSwitchBetweenPieAndSIE(bool isSimulate) | |
{ | |
UpdateUIVisibility(); | |
} | |
#endif | |
void ARTMHUDBase::UpdateUIVisibility() | |
{ | |
const bool wantVisible = bShowHUD && !(bHideUIDuringSimulate && IsSimulatingInEditor()); | |
const EVisibility visibility = wantVisible ? EVisibility::SelfHitTestInvisible : EVisibility::Hidden; | |
if(auto* playerLayerWidget = GetPlayerLayerWidget()) | |
playerLayerWidget->SetVisibility(visibility); | |
} | |
SWidget* ARTMHUDBase::GetPlayerLayerWidget() const | |
{ | |
const FName playerLayer("SPlayerLayer"); | |
// TODO - investigate if this works for local multiplayer, as it's unclear if this will find the Player Layer for this player, or the first one it finds | |
const ULocalPlayer* localPlayer = Cast<ULocalPlayer>(PlayerOwner->Player); | |
const UGameViewportClient* viewportClient = localPlayer ? localPlayer->ViewportClient.Get() : nullptr; | |
auto viewportWidget = viewportClient ? viewportClient->GetGameViewportWidget() : TSharedPtr<SViewport>(); | |
return viewportWidget.IsValid() ? RTMHUDBaseStatics::FindWidgetByTypeRecursive(viewportWidget.Get(), playerLayer) : nullptr; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "CoreMinimal.h" | |
#include "GameFramework/HUD.h" | |
#include "RTMHUDBase.generated.h" | |
UCLASS() | |
class RTMCOMMON_API ARTMHUDBase : public AHUD | |
{ | |
GENERATED_BODY() | |
public: | |
SWidget* GetPlayerLayerWidget() const; | |
protected: | |
// True to hide the UI when you hit F8 to spectate in PIE | |
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Simulation", meta=(DisplayName="Hide UI During Simulate")) | |
bool bHideUIDuringSimulate = true; | |
virtual void BeginPlay() override; | |
virtual void ShowHUD() override; | |
#if WITH_EDITOR | |
static bool IsSimulatingInEditor(); | |
virtual void HandleSwitchBetweenPieAndSIE(bool isSimulate); | |
#else | |
static bool IsSimulatingInEditor() { return false; } | |
#endif | |
void UpdateUIVisibility(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I discovered that the gameplay debugger calls ShowHUD to hide hud elements when activated (mostly to hide on-screen PrintStrings), which can be a serious problem if you need in-game UI alongside the GameplayDebugger. You can work around this by monitoring gameplay debugger state. Not included in the above example as I've implemented it using my own helper libraries, but example code follows