Last active
January 16, 2026 18:29
-
-
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 hidden or 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
| // The MIT License | |
| // Copyright (c) Richard Meredith AB | |
| // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| #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 hidden or 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
| // The MIT License | |
| // Copyright (c) Richard Meredith AB | |
| // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| #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(); | |
| }; |
Author
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