Trying to add a Widget to my C++ class

I’m trying to add a widget (that I have already created) into my C++ class.
If I could work with blueprint it would be as easy as creating a variable and setting it as the desired widget in the BeginPlay event, but I have to do this as a C++ class…
As far as i could reach is this code line:


but this won’t compile…

this is the Log

this is where the widget is created
Thanks in advance for any help.

In the path, remove everything before /Game/, and remove duplicated name at the end, leave only TEXT("/Game/ThirdPersonCPP/Blueprints/W_MinijuegoGallos")


still same error (I tried with and withouth " / ", and " ’ " but nothing changes)

You definitely don’t need " ’ ", and " / " is optional; this is what I have tested and it works:
static ConstructorHelpers::FClassFinder<UUserWidget> TestClassFinder(TEXT("/Game/Widgets/BP_ControlWidget"));

However, if you’re creating a blueprint based on this C++ class, you can use

UPROPERTY(EditAnywhere)
TSubclassOf<YourC++WidgetClassName> WidgetClass;

Set it in your BP and use it instead of ConstructorHelpers.

2 Likes

Sorry for taking so long to respond, I had to abandon this project, anyway, I can’t make it work, I think i followed your instructions but nothing seems to work…

With:
imagen
I get the following as an error:


Which i dont understand, cause I do have the blueprint called “W_MinijuegoGallos”
imagen

I guess this could work if I create a C++ UserWidget class, but what I want to do is having a Blueprint UserWidget, and then adding that BP to my C++ ActorComponent class as a variable to acces its (BP UserWidget’s) texts… images… and change them in the C++ ActorComponent class via code.

Sorry if I’m making a mess, its hard for me to explain this in english.

Thank you for your time and responses anyway.

Do you mean you want to add a BP Widget to your c++ Widget?
If so , simply create a c++ UUserWidget called TestWidget with the following property:

UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
  UUserWidget* SomeWidget = nullptr;

Then create a blueprint widget, inheriting from the c++ TestWidget called BP_TestWidget. BP_TestWidget will now require a widget called “SomeWidget” to be present because the c++ UPROPERTY has meta = (BindWidget). This property will automatically be bound to the BP widget and an error will show on BP compilation if it is not present.

But I need to acces to the different variables of the UserWidget in the code.
If i understood your method, the variable im creating:
imagen
Is going to be empty, and then “fulled” in the editor with a BP UserWidget

I need a User widget that has the variable “name” for example. Then in the AC C++ class, have a variable that saves this UserWidget as “widget” for example.
Like this I can acces to “name” from “widget” directly from the AC C++ class.

When you do follow the instructions on the previous post, you will be able to access properties on the BP UUserWidget like normal, using the Property “SomeWidget” its functions.

SomeWidget->GetXXXXXX();

It’s set to nullptr in c++ but because of its meta specifier BindWidget it will bind to an existing UUserWidget in BP and can be accessed in c++ as usual. it will not be nullptr if the deriving BP class has a widget in its widget hierarchy with the same name as the property.

*Edit

Wait is AC Actor Component? Edit :crazy_face: The example I gave with the BindWidget meta is used in widgets with a widget hierarchy.

You could also create a widget from c++ ActorComponent and store it in a UPROPERTY though:

SomeWidget = CreateWidget<USomeWidget>(this, YourBPWidgetClass);
SomeWidget ->AddToViewport(0);

//SomeWidget->GetXXXXXXXXXX();
// etc.

Note: this “YourBPWidgetClass” can be a property itself, a BP class inheriting from “USomeWidget” (c++). It can also be made configurable from a DataTable or DeveloperSettings which makes it very easy to set up references to UAssets from c++.

I’m sorry but I’m completly lost… I followed all your steps:
Created a C++ UserWidget class (TestWidget)
imagen
Created a BP inherited from TestWidget (W_Batalla)
imagen
Then tried to add a text box to W_Batalle and I can’t compile it:

And even if I could compile I need to get this BP W_Batalla into an ActorComponent C++ class, so i can type “W_Batalla->Get(NameText)” which i have no idea how:
imagen

Once again sorry for all the inconveniences…

Yes well the “BindWidget” meta works on UUserWidget classes because they have a widget hierarchy but for some reason I missed that you want to do this on an Actor Component. So sorry for that, we are going to do things a bit differently and I will show you how. I’m going to do this from my memory though:

TestComponent.h

UCLASS(ClassGroup = CommonLogic, editinlinenew, meta = (BlueprintSpawnableComponent))
class UAC_Test : public UActorComponent {
	GENERATED_BODY()

private:

UPROPERTY()
  USomeWidget* SomeWidget = nullptr;

protected:
 
// "SomeWidgetClass" is meant to be set on a BP class inheriting from UAC_Test, on the editor panel. 
UPROPERTY(BlueprintReadOnly, EditAnywhere)
  TSubclassOf<USomeWidget> SomeWidgetClass = nullptr;

public:

  virtual void BeginPlay() override;

}

TestComponent.cpp

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

  if (IsValid(SomeWidgetClass)) {
    SomeWidget = CreateWidget<USomeWidget>(this, SomeWidgetClass);
    SomeWidget->AddToViewport(0);

  // After this point you can just get your data:
  // SomeWidget->GetXXXXXXX();
  }
}

If USomeWidget does not exist in c++ yet, make a USomeWidget in c++ it makes things easier. Then create a BP asset like BP_SomeWidget (inheriting from USomeWidget) and reference the BP_SomeWidget class in the SomeWidgetClass property of UAC_Test

I think I made just what you said:


… and I got this insane amount of errors:

I don’t know if I missed something at some point…

That’s because the compiler doesn’t know what UTestWidget is, and you don’t help it =)

.h:
TSubclassOf<class UTestWidget> SomeWidgetClass;

.cpp:
#include "TestWidget.h"

The errors point at some mistakes which are c++ basics.

MiniJuegoGallos.h: Line25 / 34: UTestWidget is invalid because its header file has not been included. (this would also solve the cpp file on this error)
MiniJuegoGallos.cpp: Line23: variable SomeWidget is not declared. Add this as a UPROPERTY to the .h file.

Ok… now i get this, which I have never seen:



Means one of the arguments is not supported by the method CreateWidget.
As it says CreateWidgetInstance its first parameter is not of type ActorComponent, so you can’t pass “this” into it if this is actorcomponent. you should pick one of the other available types

What is the class where you create this widget? this doesn’t always work. A widget’s owner must ultimately be a player controller; so you need to specify your player controller instead of this.
this is okay if you create a widget in the controller class or in another widget class.

Pffff, i’ve done it guys, I used UGameplayStatics::GetPlayerController(GetWorld(), 0) thank you very much for your time and patience.

1 Like