How to bind a UTextBlock

Hi,
I’d like to bind a UTextBlock to some UObject member but I’m not sure how exactly to do it.
At the moment I have this:

UTextBlock *label = NewObject<UTextBlock>(outer, UTextBlock::StaticClass());
label->SetText(text);

which works fine but I want to change the text dynamically at runtime.
I can see in the UTextBlock doc that there is a member “TextDelegate” but it’s not clear to me how exactly the code should look like.
Can you guys give me an example please?
Thanks!

Same as any other delegate, but because it’s not multicast delegate you need to use BindDynamic instead of AddDynamic

label->TextDelegate.BindDynamic(this,&USomeFancyClass::SomeFunction());

Argument where you got “this” is a object on which function will be called, and “this” means object which function you coding in belongs to.

Now, delegates can expect different functions, currently API reference very lacking on this info, so best way to figure out what function to use is by searching delegate type (in this case FGetText) in UE4 github and usally delegate decleration will be in one of results as delegates are usally declered at begining of source files, they look like this:

DECLARE_DELEGATE_RetVal(FText, FGetText);

https://github.com/EpicGames/UnrealEngine/blob/311e18ff369078e192a83f27834b45bdb288168a/Engine/Source/Runtime/UMG/Public/Components/Widget.h#L102

In this case you need to make function without any arguments (not OneParam or somethign like that in name) and return FText value (has RetVal in name and FText in begining is type of return). If you have MULTICAST in name it means it’s multiucast delegate and it can bind more then 1 function and you use AddDynamic, if it missing like in this case it a single cast delegate and it can have only one function binded, if you bind again function will be replaced and you use BindDynamic insted. Obviously single-cast delegate is needed to set up return value delegate.

Thanks, this worked.
Can you clarify why I should use BindDynamic() if this is not a dynamic delegate? Also what is the difference between dynamic and regular delegates?

that they can be serialized, here you have docs about delegates:

i dont get how to get this done ;( and i cant find examples.

TestTextBlock->TextDelegate.BindDynamic(~~) ;

Must Call After

TestTextBlock->SynchronizeProperties();

2 Likes

I don’t know how it was before, but just in case I want to draw your attention to the fact that in version 4.25, brackets are not needed.
label->TextDelegate.BindDynamic(this, &USomeFancyClass::SomeFunction);

I did it here and it worked:

  1. I used the TextDelegate property from the UTextBlock class.
  2. This property expect a FText as a return, so the function you bind must return this type and you have to make all conversions for that.
  3. My example below:

Initialization an pointer protection for the Text:

/* 18 Pointer protection PowerBarPercentText */
	if (PowerBarPercentText)
	{
		PowerBarPercentText->SetText(FText::FromString(""));		
		PowerBarPercentText->TextDelegate.BindUFunction(this, "GetPowerBarPercentageString");
	}

The function that was binded, returning the power percent as the content for the text

FText UIceBreakerUserWidgetInGame::GetPowerBarPercentageString()
{
	if (MyClassThatHaveTheFuncion)
	{
          return FText::FromString(FString::SanitizeFloat(MyClassThatHaveTheFuncion->HitPowerPercent));
	}
	else
	{
		return FText::FromString("");
	}
}

thx a lot,it works.