Acces text from UMG userwidget from c++

I have my userwidget referenced in my user code like this:

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = “Blueprint Widgets”, Meta = (BlueprintProtected = “true”))
TSubclassOf< class UUserWidget> EnterPinHUDClass;

But I want to acces a editable text box inside of that userwidget:

How do I do that?

Sadly, you wont be able to, with current setup.
What you can do, however is :

  1. Define this somewhere accessible (PlayerController or HUD classes)

    UFUNCTION(BlueprintCallable, Category =“Widget Interaction”)
    void GetPINcode(FText inputText);`

  2. The you can call this function from your blueprints, and feed it the value from your text box.

  3. Proceed to do what you want with the value in C++.

Mark as answer if is helpful.

You can define a class derived from UUserWidget that holds an FText value, and then link your text box in UMG to this variable (link it directly to the variable rather than calling a function, it’ll save a little on runtime).

// in MyTextWidget.h
UCLASS(BlueprintType, Blueprintable)
class MYGAME_API UMyTextWidget : public UUserWidget
{
	GENERATED_BODY()

public:

	/** FText value displayed by this widget */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Text")
	FText DisplayText;

	/** Retrieves FText value currently held in DisplayText */
	UFUNCTION(BlueprintPure, Category = "Widgets|Text")
	FText GetDisplayText() const;

	/** Assigns passed FText to DisplayText */
	UFUNCTION(BlueprintCallable, Category = "Widgets|Text")
	void SetDisplayText(const FText& gmt_NewDisplayText);

};


// in MyTextWidget.cpp

/** Retrieves FText value currently held in DisplayText */
FText UMyTextWidget::GetDisplayText() const {
	return DisplayText;

}

/** Assigns passed FText to DisplayText */
void UMyTextWidget::SetDisplayText(const FText& NewDisplayText) {
	DisplayText = NewDisplayText;

}

After you’ve defined the class, you can reparent your UMG blueprint to this class under Class Settings.

http://images.gigasightmedia.com/GMT_graphics/UE4Tutorials/UMGText/Class-Settings.png

http://images.gigasightmedia.com/GMT_graphics/UE4Tutorials/UMGText/CustomProperties.png

http://images.gigasightmedia.com/GMT_graphics/UE4Tutorials/UMGText/UMGDesigner.png

http://images.gigasightmedia.com/GMT_graphics/UE4Tutorials/UMGText/UMGBinding.png

If you’re using an editable text input, you can build a method into the OnTextChanged event that assigns the new value to this variable.

http://images.gigasightmedia.com/GMT_graphics/UE4Tutorials/UMGText/UMGOnTextChanged.png

http://images.gigasightmedia.com/GMT_graphics/UE4Tutorials/UMGText/UMGSetDisplayText.png

Your other option is to place a pointer to the UEditableText widget (or whichever kind of widget you’re using) in your class, and then assign the text box you’ve created in UMG to this pointer; then you can access the FText value of that text box directly.

Bear in mind that in some of the native text widgets the Text value is protected.

//MyTextWidget.h

/** Text box displayed by this widget */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Text")
UEditableTextBox* TextBox;


//MyTextWidget.cpp

/** Retrieves FText value currently displayed by TextBox*/
 FText UMyTextWidget::GetDisplayText() const {
     if (TextBox) {
          return TextBox->Text;

     }
 
 }
 
 /** Assigns passed FText to TextBox display */
 void UMyTextWidget::SetDisplayText(const FText& NewDisplayText) {
     if (TextBox) {
         TextBox->Text = NewDisplayText;

     }
 
 }

http://images.gigasightmedia.com/GMT_graphics/UE4Tutorials/UMGText/UMGSetEditableTextBox.png

If you already have a UUserWidget subclass defined, you can just add these properties to the existing class.

In order to access the class properties from your array, you’d have to first cast the object to the UMyTextWdiget class.

Thanks! gonna try this soon when I got the time.

No problem.
If you expand on this technique you can build menus with complex functionality that still allow designers to completely change the graphical appearance/layout just by assigning their UMG widgets to class variables. It’s very powerful. My entire menu system is built on this.

How do I get reference to that class I created, I want to call functions from my user widget class from my character class

I want the content of a textbox to be the value of a variable in my character class, right now my code looks like this but the function only gets called one time :confused:

FText UMyUserWidget::GetDisplayText() const {
	AEscapeDemoGameMode* gameModeObject = (AEscapeDemoGameMode*)GetWorld()->GetAuthGameMode();
	AEscapeDemoCharacter* MyCharacter = Cast<AEscapeDemoCharacter>(UGameplayStatics::GetPlayerCharacter(gameModeObject, 0));
	return FText::FromString(MyCharacter->GetInteractText());

}

What I want to do is:

In my character class:

UMyUserWidget myWidget = (how do I get this instance?);
myWidget->myFunction;

There’s a number of ways you can do this; which is most appropriate depends on how you are building, spawning, and using the widget(s).

Assuming you have a reference somewhere to the widget object that’s created, you can pass that reference to your character when you spawn the widget; or if you’re creating the widget from within the character you already have it and just need to store it.

You could also create an event that the character calls to update the values that all instances of the widget respond to.

You could also have your widget pass itself to the character upon construction, similar to the code you have to GetDisplayText, wherein the “AssignTextWidget” method below would store the reference in a single or array of UMyUserWidget*:

 voidUMyUserWidget::AssignToCharacter() const {
    AEscapeDemoGameMode* gameModeObject = (AEscapeDemoGameMode*)GetWorld()->GetAuthGameMode();
    AEscapeDemoCharacter* MyCharacter = Cast<AEscapeDemoCharacter>(UGameplayStatics::GetPlayerCharacter(gameModeObject, 0));
    if (MyCharacter) {
        MyCharacter->AssignTextWidget(this);

    }
 
 }

Or you could have the containing menu or HUD perform similar tasks for contained widgets to associate them with the appropriate character values when it is spawned/configured.

I got a reference to my widget referencing it from my gamemode blueprinted.

Now I got a different problem, for some reason I can’t change my Text object “DisplayText”. I causes my editor to crash.

I’m using the following code:

gameModeObject->GetIngameHUD()->DisplayText = FText::FromString(TEXT("test"));

http://images.gigasightmedia.com/GMT_graphics/UE4Tutorials/UMGText/UMGBinding.png

You should probably put this in a new question; it’s best to keep different questions in separate posts so others looking with similar problems can more easily find answers.

Does the GetIngameHUD() method actually return the widget? What error message are you getting? I would guess it’s a nullptr from the looks of it.

I finally got it to work. I was trying to cast a TSubclassOf< class UMyUserWidget > to a UMyUserWidget which made it a nullptr.

IngameHUDClass.GetDefaultObject();

Did the trick for me :slight_smile:

Thanks alot for your help