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.