x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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: alt text

How do I do that?

Product Version: UE 4.13
Tags:
more ▼

asked Oct 25 '16 at 09:48 AM in C++ Programming

avatar image

Kamarov
8 4 5 9

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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.

Reparent the UMG Blueprint

Custom properties are available in the graph

Designer Hierarchy

Bind the TextBox to your DisplayText

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.

OnTextChanged event

Set Display Text

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;

      }
  
  }

Set UMG text box to C++ pointer

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.

more ▼

answered Oct 25 '16 at 01:53 PM

avatar image

GigasightMedia
1.7k 43 13 61

avatar image Kamarov Oct 25 '16 at 02:34 PM

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

avatar image GigasightMedia Oct 25 '16 at 02:49 PM

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.

avatar image Kamarov Nov 23 '16 at 12:08 PM

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 :/

 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;

avatar image GigasightMedia Nov 29 '16 at 05:39 AM

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.

avatar image Kamarov Nov 29 '16 at 12:07 PM

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"));

alt text

avatar image GigasightMedia Dec 01 '16 at 05:33 PM

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.

avatar image Kamarov Dec 03 '16 at 02:15 PM

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

Thanks alot for your help

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Oct 25 '16 at 11:50 AM

avatar image

Holybreath
383 10 7 19

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question