0

From code I want to assecc widget and and send string variable to it. But with my code I see only default value for it. Whet I set value in widget without code - it works.

Call method from another class:

  UChangeDialogBottomText* changeDialogBottomText = CreateWidget<UChangeDialogBottomText>(
    GetWorld()->GetFirstPlayerController(), UChangeDialogBottomText::StaticClass());
  if (changeDialogBottomText)
  {
    changeDialogBottomText->UpdateDialogBotton();
  }

.h

UCLASS()
class HOME_API UChangeDialogBottomText : public UUserWidget
{
  GENERATED_BODY()
  
public:
  UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (BindWidget)) FString st_Name_Key;

  UFUNCTION()
  void UpdateDialogBotton();
};

.cpp

void UChangeDialogBottomText::UpdateDialogBotton()
{
  st_Name_Key = "core";
}

enter image description here

enter image description here

1 Answer 1

1

The Event Construct pin the in the blueprint is only called once when the widget is created, not every time a variable changes. Therefore, if you want the UpdateDialogBotton method to change the text of the widget, then you have to call the SetText method in there. However, you will also need a reference to the text block widget in C++. Right now, you have a reference to it in blueprints. There are many ways to go about this, but the easiest way would be to make a new property in your header file like so,

class UTextBlock; // <-- new line

UCLASS()
class HOME_API UChangeDialogBottomText : public UUserWidget
{
  GENERATED_BODY()
  
public:
  UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (BindWidget)) // <-- new line
  UTextBlock* name_Text_Block; // <-- new line

  UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (BindWidget)) 
  FString st_Name_Key;

  UFUNCTION()
  void UpdateDialogBotton();
};

And then in that event construct method in the blueprint, assign the name_Text_Block C++ variable to the Name blueprint variable. After doing that, you can change the text of the widget through the UpdateDialogBotton method like so,

void UChangeDialogBottomText::UpdateDialogBotton()
{
  st_Name_Key = "core";
  if (name_Text_Block) {
    name_Text_Block->SetText(FText::FromString(st_Name_Key));
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks. Now I need to send value of the variable from code to the widget, but is don't work. I can see id using printscreen like on the second screenshot.
@DimaKozyr Remember, Event Construct gets called when the widget is created, so no matter what you change the st name key variable to in the UpdateDialogBotton method, you won't see the updated value in Event Construct. You need to invoke your own blueprint function after UpdateDialogBotton is called or the blueprint Tick function, just for testing only, to see the new updated value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.