# Referencing UMG Class Variables in C++?

**URL:** <https://forums.unrealengine.com/t/referencing-umg-class-variables-in-c/43879>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [October 6, 2015, 9:55am UTC](https://forums.unrealengine.com/t/referencing-umg-class-variables-in-c/43879 "2015-10-06T09:55:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Commander\_Shepard](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Commander\_Shepard](https://forums.unrealengine.com/u/Commander_Shepard)\
**Post date:** [October 6, 2015, 9:55am UTC](https://forums.unrealengine.com/t/referencing-umg-class-variables-in-c/43879/1 "2015-10-06T09:55:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_6334e343](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_6334e343](https://forums.unrealengine.com/u/anonymous_user_6334e343)\
**Post date:** [October 6, 2015, 12:10pm UTC](https://forums.unrealengine.com/t/referencing-umg-class-variables-in-c/43879/2 "2015-10-06T12:10:38Z")

</div>

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

```auto

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.

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

---

<div class="post-metadata">

**Author:** ![IrishKilter](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@IrishKilter](https://forums.unrealengine.com/u/IrishKilter)\
**Post date:** [October 6, 2015, 4:03pm UTC](https://forums.unrealengine.com/t/referencing-umg-class-variables-in-c/43879/3 "2015-10-06T16:03:44Z")

</div>

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

**YourProjectName.Build.cs**

```auto

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)**

```auto

	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**

```auto

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!

---

<div class="post-metadata">

**Author:** ![Commander\_Shepard](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Commander\_Shepard](https://forums.unrealengine.com/u/Commander_Shepard)\
**Post date:** [October 7, 2015, 11:10am UTC](https://forums.unrealengine.com/t/referencing-umg-class-variables-in-c/43879/4 "2015-10-07T11:10:41Z")

</div>

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