Bind Property or Function to ProgressBar

I have just begun palying with Unreal Engine and as i am a Computer Science student i what to learn as mutch C++ as i can. So my question is how do i bind a Property or Function to a ProgressBar in C++. I have some what of a solution but i just can’t figure out the last pice of the puzzle. Here is the .cpp file of the widget class i have made.

#include "LegendOfTroelsMegaT.h"
#include "SPlayerStatsWidget.h"
#include "SProgressBar.h"
#include "SlateCore.h"

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SPlayerStatsWidget::Construct(const FArguments& InArgs)
{
	WidgetHeight = 50.0f;
	WidgetWidth = 500.0f;

	OwnerHUD = InArgs._OwnerHUD;
	PlayerCharacter = InArgs._Character;
	ChildSlot
		.VAlign(VAlign_Top)
		.HAlign(HAlign_Left)
		[
			SNew(SBox).HeightOverride(WidgetHeight)
			.WidthOverride(WidgetWidth)
			[
				SNew(SVerticalBox)
				+ SVerticalBox::Slot()
				[
					SNew(SProgressBar)
					.Visibility(EVisibility::Visible)
					.Percent_UObject(PlayerCharacter, &ALegendOfTroelsMegaTCharacter::GetPercentHealth)
					
				]
			]
			
		];
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION

The problem here is that i can’t figure out how to use the Percent Attribute of the ProgressBar

Hi,

As far as PlayerCharacter is a pointer: ALegendOfTroelsMegaTCharacter* PlayerCharacter; (long class name is long) it should work just fine. Do you get any errors during compilation or it just doesnt work? Can you paste ALegendOfTroelsMegaTCharacter::GetPercentHealth function?

the error i get is something like this: error C2664: ‘SProgressBar::FArguments::WidgetArgsType &SProgressBar::FArguments::Percent_UObject(UserClass ,TOptional (__cdecl ALegendOfTroelsMegaTCharacter:: )(void) const)’ : cannot convert argument 2 from ‘TOptional (__cdecl ALegendOfTroelsMegaTCharacter::* )(void)’ to ‘TOptional (__cdecl ALegendOfTroelsMegaTCharacter::* )(void) const’

I have tried to mess a little arround with the return value of the function to acomodate the error but here it is…

`TOptional GetPercentHealth(){ return CurrentHealth / MaxHealth; }
private:
UPROPERTY(EditAnywhere, Category = Stats)
float MaxHealth;

UPROPERTY(EditAnywhere, Category = Stats)
	float CurrentHealth;`

and this is from the Character.h file

Your GetPercentHealth needs to be const to work with Slate

Thanks now it compiles :slight_smile:

To restate my comment as an answer, when binding a function to a slate widget, the method must have the const specifier.

That is, in your instance:

TOptional GetPercentHealth();

Should be declared:

TOptional GetPercentHealth() const;

That is, it won’t change the instance by calling this method.