Slate data binding issue

I am trying to learn slate and have tried making a progress bar that should reflect my good old character hunger variable.

But I cannot get it to work right, can someone help?

This is the current code

void SPlayerHUDUI::Construct(const FArguments& args)
{
	OwnerHUD = args._OwnerHUD;

	Hunger.Bind(this, &SPlayerHUDUI::GetHunger);

	ChildSlot
		[
			SNew(SOverlay)
			+ SOverlay::Slot()
				.HAlign(HAlign_Center)
				.VAlign(VAlign_Top)
				[
					SNew(SBox)
					.WidthOverride(1000.f)
					.HeightOverride(100.f)
					[
						SNew(SProgressBar)
						.Percent(Hunger.Get())
					]
				]
		];

}

float SPlayerHUDUI::GetHunger() const
{
	return  Player->Hunger;
	
}

.H

TAttribute<float> Hunger;

float GetHunger() const;

This will work and show the initial value on the progress bar but does not dynamically update, can anyone tell me why not? (The actual Player::Hunger variable updates every tick)

That’s because you called Get on your TAttribute when passing the value in to the widget. This essentially bakes down the TAttribute to its current value, and then constructs another TAttribute based on that literal value (rather than bound to the function).

You’ll want to do this (also note that you don’t need to keep the TAttribute around in the header):

SNew(SProgressBar)
.Percent(this, &SPlayerHUDUI::GetHunger)

Thanks for the swift response!
I was already suspecting that .Get() would break the bind, but I’m very new to c++ and coding in general so this info makes things a little more clear, thanks for this answer.

Ps. I had to change the function’s return type from a float to TOptional to make this work, is this the correct approach?

Yeah, that’s correct.

If you check SProgressBar.h you can see the definitions for all of its attributes and arguments, and you’ll note that the Percent attribute is a TOptional. This is so the progress bar can marquee if you don’t know the current progress amount (by returning an empty TOptional).