Skip to content

Instantly share code, notes, and snippets.

@rtm223
Created June 12, 2024 18:42
Show Gist options
  • Save rtm223/99ef8641d7b8882adfa98077554ffc58 to your computer and use it in GitHub Desktop.
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
#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;
}
#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();
};
@rtm223
Copy link
Author

rtm223 commented Jun 13, 2024

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

bool URTMDbgGameplayDebuggerMonitorSubsystem::GetGameplayDebuggerEnabledState(const APlayerController* playerController) const
{
	if(playerController)
	{
		const auto& playerManager = AGameplayDebuggerPlayerManager::GetCurrent(GetWorld());
		if(const auto* replicator = playerManager.GetReplicator(*playerController))
			return replicator->IsEnabled();
	}
	return false;
}```

This code needs to be excluded in Shipping and Test Builds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment