Download

How to bind a c++ variable to widget blueprint

i’m kinda new to blueprint stuff and I don’t know how to bind a c++ string to the textbox in widgets. the DisplayNote is the c++ variable as u can see down here. how do I bind it so that whenever the variable changes the text block gets updated?

.h



#pragma once

#include "BindExample.generated.h"

UCLASS(Abstract)
class UBindExample : public UUserWidget
{
GENERATED_BODY()

public:
virtual void NativeConstruct() override;

UPROPERTY(BlueprintReadOnly, **meta = (BindWidget)**)
class UTextBlock* ItemTitle = nullptr;
};

.cpp


#include "BindExample.h"
#include "Components/TextBlock.h"

void UBindExample::NativeConstruct()
{
// Call the Blueprint "Event Construct" node
Super::NativeConstruct();

// ItemTitle can be nullptr if we haven't created it in the
// Blueprint subclass
if (ItemTitle)
{
ItemTitle->SetText(TEXT("Hello world!"));
}
}

More information: Connect C++ to UMG Blueprints with BindWidget · ben🌱ui

that’s not what i wanted. the class needs to inherited from AActor not widgets.

and the question was about how to do it in blueprint not c++

In your UserWidget class:


UFUNCTION(BlueprintImplementableEvent)
void UpdateMyText(const FString& MyText);

Then in your .cpp call it as:


MyAnotherFunction()
{
  UpdateMyText("MY NEW TEXT");
}

Then override it in your WidgetBlueprint, you can update your Text in the function using its parameter.

okay let me explain it better.

i have a c++ class called ANotes, this class is going to be placed in the world so that whenever the player walks into it pops up an image with text on it.

in the ANotes .h:

UPROPERTY(EditDefaultsOnly)
TSubclassOf<class UUserWidget> NoteClass;

UPROPERTY()
class UUserWidget* Note;

UPROPERTY(EditAnywhere, BlueprintReadOnly)
FString TEXTMenu;

you see that I have marked the FString as Editanywhere cuz there’s going to be more than one instance so I want to be able to edit the text on the instance without having to go back to the code.

in the cpp i have created 2 functions that are going to be called from the character class to display or hide the widget that contains the image and text.

void ANotes:displayNotes()
{
if (NoteClass)
{
Note = CreateWidget(GetWorld(), NoteClass);
Note->AddToViewport();
}
}

void ANotes::HideNotes()
{
if (Note->IsInViewport())
{
Note->RemoveFromViewport();
}
}

but what I don’t know is how to bind the TEXTMenu variable to the TextBlock that exists in the widget blueprint. I’m sure it will work cuz I have seen it in an old topic but I can’t find it now.

This will help you ( I hope ):Unreal Engine 4 Tutorial: Healthbar & Binding Variables - YouTube
It would be better if you did it the way I answered but it should work, more about UMG performance:
Drive UI Updates with Events | Unreal Engine Documentation

Create a new c++ class that inherits from UUserWidget and create a BlueprintImplementableEvent function to be called to update it’s text, then call this function from the function that you have that changes the FString.