Setting UI widgets through Hud

Sorry, sorry, I know, I am asking too many questions. Consider it me catching up to the Unreal engine, which I should have done way earlier. :stuck_out_tongue:

Anyways, as part of my ongoing quest of learning how to deal with C++ and UE4, I am now trying to add a HUD to my game. All the necessary information for UMG is already present within the game mode file.
The following code is based on a solution I found on the Unreal Answerhub, but when I implement it, it simply just crashes my editor. Immediately. Moving it to the BeginPlay (which I also tried), just makes it freeze the editor the moment I press play, so I reckon there’s something wrong on how I declared the class to begin with.

How do I go about attaching a widget to a HUD? To prevent having to setup the HUD in a level blueprint for every level, I want to have at least the initialization taking place within HUD class.

CPP


// Fill out your copyright notice in the Description page of Project Settings.

#include "MyGame.h"
#include "BK_HUDBase.h"
#include "BP_UIWidget_Minimap.h"


ABK_HUDBase::ABK_HUDBase(const FObjectInitializer& ObjectInitializer)
	:Super(ObjectInitializer)
{
	HUDMinimapClass = LoadClass<UBP_UIWidget_Minimap>(NULL, NULL, TEXT("This/Is/A/Path/TestMiniMap.TestMiniMap'"), LOAD_None, NULL);
	HUDMinimap = CreateWidget<UBP_UIWidget_Minimap>(GetWorld(), HUDMinimapClass);
	HUDMinimap->AddToViewport();
}

void ABK_HUDBase::BeginPlay()
{
}

H



// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/HUD.h"
#include "BP_UIWidget_Minimap.h"
#include "BK_HUDBase.generated.h"

/**
 * 
 */
UCLASS()
class MYGAME_API ABK_HUDBase : public AHUD
{
	GENERATED_BODY()

	ABK_HUDBase(const FObjectInitializer& ObjectInitializer);

	TSubclassOf<class UBP_UIWidget_Minimap> HUDMinimapClass;
	UBP_UIWidget_Minimap* HUDMinimap;

	virtual void BeginPlay() override;
	
};



I have a feeling it has to do with one of the other 4 parameters of LoadClass, since I only knew what to fill in for 1 of them.

Bonus question How do I go about changing the resolution size of the widget? Preferably, I want to spit up the HUD into various parts, since that would be easier to maintain for what I am planning. That, or learning how to mask the contents of widgets would be awesome. Since I am building a minimap, it would be annoying if visual indicators could slip beyond the borders of the minimap.

This tutorial should help you create widget from C++: https://forums.unrealengine.com/showthread.php?52773-Tutorial-Snippet-Creating-a-UMG-Widget-in-C-and-delegate-example

Thanks man! I was indeed able to get that to work. You wouldn’t happen to know how I could move this functionality effectively into the HUD though, would you? I prefer to have all my HUD related functionality within the player HUD to be honest, and I also wish to use some of the canvas related functionality later on as well.

1. Place this variables in your HUD class instead of PlayerController class



	// The class that will be used for the players Inventory UI
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Player, HUD and UI")
		TSubclassOf<class UInventoryUserWidget> InventoryUIClass;

	// The instance of the players Inventory UI Widget
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Player, HUD and UI")
		class UInventoryUserWidget* InventoryWidget;


2. Override the BeginPlay() method of your HUD class



void AMyHUD::BeginPlay()
{
	 Super::BeginPlay();

         APlayerController* PC = GetWorld()->GetFirstPlayerController(); //You can also use GetOwningPlayerController() method from AHUD class

	// Only create the UI on the local machine (dose not excist on the server.)
	if (PC && PC->IsLocalController())
	{
		if (InventoryUIClass) // Check the selected UI class is not NULL
		{
			if (!InventoryWidget) // If the widget is not created and == NULL
			{
				InventoryWidget = CreateWidget<UInventoryUserWidget>(PC, InventoryUIClass); // Create Widget
				if (!InventoryWidget )
				    return;
				InventoryWidget->AddToViewport(); // Add it to the viewport so the Construct() method in the UUserWidget:: is run.
				InventoryWidget->SetVisibility(ESlateVisibility::Hidden); // Set it to hidden so its not open on spawn.
			}
		}
	}
}


That should help you achieve what you want. :slight_smile:

Thanks!

…heh, looking it over, I should have been able to guess this by myself :slight_smile: