How to bind a c++ variable to widget blueprint

I know its very old. BUT I wanted to Bind a float to by ProgressBar in a Widget using the UE Delegates. This is what I found, use and its working:

	if (Player_Energy_Widget_Class != nullptr) {
		Player_Energy_Widget = CreateWidget(GetWorld(), Player_Energy_Widget_Class);
		Player_Energy_Widget->AddToViewport();

		UWidget* obj = Player_Energy_Widget->GetWidgetFromName("ProgressBar_0");
		Player_Progress_Bar = Cast<UProgressBar>(obj);

		if (Player_Progress_Bar == nullptr) {
			UE_LOG(LogTemp, Warning, TEXT("No Progressbar_0 found in the Widget"));
		}
		else {
			Player_Progress_Bar->PercentDelegate.BindUFunction(this, "GetEnergy");
			Player_Progress_Bar->SynchronizeProperties();
		}
	}

The “Get Energy” Function:

float APlayer::GetEnergy()
{
	return Energy * 0.01f;
}

For this function i used the UFUNCTION Property.

The key for this to work is the “Player_Progress_Bar->SynchronizeProperties();”
Call it after you set your Bindings! If you miss it, the Bindings and other Stuff are not working.

3 Likes