Referencing UMG Class Variables in C++?

UE 4.9.2 Launcher Downloaded

Hi I want to be able to reference UMG Widget Class as variables in C++ which will then be populated by the derived Blueprint.

I am not handling UMG behaviour and layout or anything in C++ I just want to get a reference to a Blueprint UMG Widget Variable in C++ Expose that variable to Blueprints to set its value and then display it or hide it as necessary from C++.

So I search and found that you can indeed do it but you need to put Extra modules in Build.cs files namely UMG,Slate and SlateCore, but hell I can’t find any Build files. I remember reading that it was deprecated in favor of Target files.

Unfortunately I can’t find enough info on where to put modules in Target files.

You add the modules in YourProjectName.Build.cs
What you want for the refrence is a



UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "My Widget, Sub My Widget")
UUserWidget* MyUMGWidget;


There is a wiki on extending user widget that may be of some help.

You want to add UMG to Public, and Slate and SlateCore to Private like so:

YourProjectName.Build.cs



public class Blight : ModuleRules
{
	public Blight(TargetInfo Target)
	{
        PublicDependencyModuleNames.AddRange(new string] { "Core", "CoreUObject", "Engine", "InputCore", "AIModule", **"UMG",** });

		PrivateDependencyModuleNames.AddRange(new string] {  });

		// Uncomment if you are using Slate UI
		 PrivateDependencyModuleNames.AddRange(new string] {** "Slate", "SlateCore"** });
		
		// Uncomment if you are using online features
		// PrivateDependencyModuleNames.Add("OnlineSubsystem");
		// if ((Target.Platform == UnrealTargetPlatform.Win32) || (Target.Platform == UnrealTargetPlatform.Win64))
		// {
		//		if (UEBuildConfiguration.bCompileSteamOSS == true)
		//		{
		//			DynamicallyLoadedModuleNames.Add("OnlineSubsystemSteam");
		//		}
		// }
	}
}


YourClass.h (controller in this case)



	UPROPERTY() //The class (could also be done in a local function for a one-time go)
	TSubclassOf<UUserWidget> CharacterSelectWidgetClass;	
        
        UPROPERTY() //The instance (your actual reference)
	class UUserWidget* CharacterSelectWidget;


YourClass.cpp




ABLTPlayerController::ABLTPlayerController(const FObjectInitializer& ObjectInitializer)
:Super(ObjectInitializer)
{
	static ConstructorHelpers::FClassFinder<UUserWidget> BlueprintClass(TEXT("/Game/UI/Menus/BP_UI_Menus_CharacterSelectScenario")); //This is the path to your content. You can right-click -> Copy Reference Link inside the Content Browser, but make sure to remove the duplicated naming when pasting. "CharacterSelectScenario.CharacterSelectScenario" won't compile.
	
	if (BlueprintClass.Class)
		CharacterSelectWidgetClass = BlueprintClass.Class;


}

void ABLTPlayerController::OpenMenu(TSubclassOf<UUserWidget> MenuClass,bool bUseMouse)
{
	if (!MenuClass) //Missing widget class, can't continue
		return;

	UUserWidget* Menu = CreateWidget<UUserWidget>(this, MenuClass);

	if (!Menu) //Failed to create widget
		return;

	Menu->AddToViewport();
	bShowMouseCursor = bUseMouse;
	
}


Done!

Thanks man, Although instead of “class UUserWidget*” just “UUserWidget*” works fine too