Creating a Widget in just C++

Hi I am trying to make a puzzle game where you need to enter a password into a textbox to open a door. I saw a load of examples online using blueprint but I want to try doing it with just c++ alone but can not find much learning resources online other then some old stuff from a couple of years ago.

I am currently using c++ in my project to open doors and activate triggers and the only decent resource I have found is this UEditableTextBox | Unreal Engine Documentation

I just want to note widgets are new to me I have not used them yet in my use of unreal as I am still learning it and what I am making is my first proper project.

So I am writing here to get the most relevant information possible on C++ and widgets and hopefully access to some helpful guides on the subject that I could learn from.

This is how I managed to spawn a UMG widget through code

https://answers.unrealengine.com/questions/756759/unable-to-create-widget-no-outer-provided.html

I created the widget in the editor and then used FClassFinder to grab that class, and then spawned it using CreateWidget.

There is a way to have your entire widget logic in C++ and only use BP for its design.

Make a C++ class which derives from UUserWidget, simply UserWidget in the create a class selector.
Make a new BP widget and in the class settings make it use your custom class as its parent.

You can create the widget with the way Cd1232 mentioned and then make a variable for each type that you need to have logic for (Text Blocks, Scroll Boxes etc) and use the UPROPERTY() specifier meta = (BindWidget) to bind the visual element that you want to handle in C++.

To bind certain delegates for your variables like Click events for buttons, you need to override the initialize method:

bool Initialize() override;

and in there bind your delegates like so:

bool UYourClass::Initialize()
{
    bool Success = Super::Initialize();
    	if (!Success) { return false; }
    
    	if (YourCustomButtonCreatedInHeader)
    	{
    		YourCustomButtonCreatedInHeader->OnClicked.AddDynamic(this, &UYourClass::OnClickedCustomButton);
    	}
    	return true;
}

And then have the on clicked logic on your custom OnClickedCustomButton() function.

Making the graphics in C++ is the old way of making UI which I strongly advise against as it is a lot of extra work for nothing.

Hi thanks for the information I tried creating the c++ class deriving from “UserWidget” as you recommended but it generated errors when I tried compiling and the class will not show up in my content browser till I compile it but when I do try and compile it same errors pop up again. Any idea why I might be getting these. I also ended up creating two c++ classes under “UserWidget” and they are both listed here.

Errors:

Linker errors are usually created when you are missing an include. In this case, I assume you need to add the UMG module in your build file.

Find your YourProject.Build.cs file and add “UMG” with the quotation marks in the Public Dependency Modules, like so:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "UMG"});

Ya I tried it and still got errors, I tried starting a new project from scratch just see if UserWidget would work if I started a new project (to keep it separate from the other stuff I did) but the class still will not compile into unreal.

What I did was:

  1. Started a new project
  2. made changes to the .build.cs file you recommended (tried compiling afterwards and got errors)
  3. created widget ui design using BP
  4. create a custom c++ class with UserWidget in the create a class selector. (errors still pop up on compile)

I feel like I am likely missing a step here or doing something wrong. I might try it all again. Sorry for the inquiry I am likely making some rookie mistake somewhere.

Sorry, didn’t quite catch that. Are you getting errors after adding the UMG module in the dependences or is it successfully added but you are still unable to compile a UUserWidget child?

Hello,

I would recommend looking into using the UUserWidget class for creating and maintaining Widget Blueprints in C++. Hope this helps.

Thanks,

Hi I started a serparate project as I ended up getting crashes so I did everything in a new project

First I added the UMG module in the build.cs file as you recommended

I then compiled and got the following

I ignored the errors and tried to create a c++ class using userwidget and got the following response which would not allow the c++ class appear in the editor.

followed by the following in the error log

I also tried making a basic c++ class with some includes I was recommended elsewhere.

Well, the error log is quite specific actually. This doesn’t have to do with the UMG module or your efforts to make a UUserWidget class. Your code doesn’t compile elsewhere. More specific, in line 24 of your MyPlayerController.h where I assume you have misspelled “Category” as “Cathegory” in your UPROPERTY() specifiers.

After looking around and checking hereI got the build file to compile by adding “slate” and “slate core” modules with “UMG” in the build.cs file followed by adding a set of includes to the main project header file.

Now I have to just get password input setup thanks for the help.

1 Like