Last active
February 12, 2023 02:57
-
-
Save JoshLmao/52348ff6a28ffb6033fce2910159f147 to your computer and use it in GitHub Desktop.
UE4 C++ HUD to add an array of widgets to the Viewport on BeginPlay - (Dont forget to add "UMG" to your project.Build.cs's PublicDependencyModuleNames property)
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 "AWidgetHUD.h" | |
#include "Blueprint/UserWidget.h" | |
AWidgetHUD::AWidgetHUD() | |
{ | |
} | |
void AWidgetHUD::BeginPlay() | |
{ | |
Super::BeginPlay(); | |
// If any widgets need to be added | |
if (AllUIWidgets.Num() > 0) | |
{ | |
// Iterate through all widgets | |
for (TSubclassOf<UUserWidget> widget : AllUIWidgets) | |
{ | |
// Create an instance of the widget and add to viewport | |
UUserWidget* createdWidget = CreateWidget<UUserWidget>(GetWorld(), widget); | |
createdWidget->AddToViewport(); | |
// Store instanced widget in array | |
m_createdWidgets.Add(createdWidget); | |
} | |
} | |
} | |
void AWidgetHUD::DrawHUD() | |
{ | |
Super::DrawHUD(); | |
} |
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 "AWidgetHUD.generated.h" | |
/** | |
* HUD to add any widgets set in the inherited blueprint to the HUD on BeginPlay | |
*/ | |
UCLASS() | |
class QUINNSESCAPE_API AWidgetHUD : public AHUD | |
{ | |
GENERATED_BODY() | |
public: | |
AWidgetHUD(); | |
/* | |
* VARIABLES | |
*/ | |
protected: | |
// All on screen widgets to add to the HUD on BeginPlay | |
UPROPERTY(EditAnywhere) | |
TArray<TSubclassOf<class UUserWidget>> AllUIWidgets; | |
private: | |
// Current list of created UI widgets that are constantly active on screen | |
TArray<class UUserWidget*> m_createdWidgets; | |
/* | |
* METHODS | |
*/ | |
protected: | |
virtual void BeginPlay() override; | |
virtual void DrawHUD() override; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment