Connecting healthbar percentage with Actors Health

I have a Actor Called Enemy that has a functioning health system
off 100 everytime I shoot it it takes off 25. after I shot that enemy 4 times it destroys itself
it al works well. but now i want to make my UUserWidget healthbar class that i have create connect to the enemy class

I have heard that you can connect it through blueprint., does it make it any difference in performace
or is it just a matter what you perfer?

this is my enemy.h

UCLASS()
class CASTLEDEFENCE_API AEnemy : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AEnemy();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	UPROPERTY(VisibleAnywhere)
		UWidgetComponent* HealthWidgetComp;

public:	

	UPROPERTY(VisibleAnywhere, BluePrintReadOnly)
		float Health = 100;

//other code...

this is my HealthBar.h

UCLASS()
class CASTLEDEFENCE_API UHealthBar : public UUserWidget
{
	GENERATED_BODY()
public:
	//void SetOwnerEnemy(AEnemy* InEnemy) { OwnerEnemy = InEnemy; };

	void setHealth(float Val) { HealthBar->SetPercent(Val); };

protected:

	UPROPERTY(EditAnywhere)
		class AEnemy* AEnemyUpdate;

	UPROPERTY(meta = (BindWidget))
	 UProgressBar* HealthBar;

	UPROPERTY(meta = (BindWidget))
	UTextBlock* CurrentHealthLabel;

	UPROPERTY(meta = (BindWidget))
	 UTextBlock* MaxHealthLabel;
	
};

The UUserWidget is attachted to the enemy blueprint.
but the healtbar percentage is not connected.

i tried to create this public function

void setHealth(float Val) { HealthBar->SetPercent(Val); };

so i could set the percentige from another class maybe with the tick function in the Enemy class

But i dont know if thats the right way of doing that? and i dont know how to call the “setHealth()” fuction in the Enemy class everytime i try this

UHealthBar* healtbar ;
healtbar->setHealth(50);

visual studio wont Build it succesfully and i get allot of error messages like:

|Error|C3859|Failed to create virtual memory for…
|Error|C1076|compiler limit: internal heap limit …

Please help me with this

The SetPercent method needs a value between 0 and 1. If you add a MaxHealth property then you can easily get that with Health / MaxHealth.

Also instead of updating your Health widget on Tick you should only update on changed, so wherever you apply damage to remove health or add health call a method to refresh the widget.

I’m not sure if the error is related to the value exceeding 1.0f however.

Alright so Have updated the function on when it changes the health

Enemy.h(actor)

protected:
UPROPERTY(EditAnywhere)
		class UHealthBar* healthbar;


public:
	UPROPERTY(VisibleAnywhere, BluePrintReadOnly)
		float Health;

	UPROPERTY(EditAnywhere)
		float MaxHealth = 120;

	float GetHealth() const { return Health; };

	UFUNCTION()
	void SetHealth(float val);

Enemy.cpp(actor)

void AEnemy::BeginPlay()
{
Super::BeginPlay();
Health = MaxHealth;
}

void AEnemy::SetHealth(float val)
{
	Health = val; // <- works fine
	healthbar->setHealth(Health / MaxHealth);// <- causes the crash
}

when the “SetHealth” function is activated (because of the projectille Onhit function)
It crashes Unreal engine so im pretty sure thats not how to adjust the UUserWidget (Healthbar)
I have used Cast before but i dont know how to use it in this situation and if it would even be the best option.

Please help me out with code with coding this better, because right now im just guessing

Presumably your healthbar is null if you can’t call a method on it.
Where is that variable set?

Separately, you probably want to bind to an event or delegate to update the health, rather than explicitly have to call a method.

Finally, it might be even easier if your health bar widget is instead bound to pull the value from its owning character. In a Widget Blueprint you can easily do this with the “Bind” widget for the progress par percentage value; put a property on the bar that’s the “owning enemy” and read the degree completed from there.

the healtbar is set under protected in the Enemy.h

protected:
UPROPERTY(EditAnywhere)
		class UHealthBar* healthbar;

I know this can be done in blueprint but im not looking for the easiest solution
I want to develop myself unreal engine programmer.

And i dont know if using blueprints for binding widget
is a very common thing for UE programmers?
and if there is any performance difference ?

Im curious please let me know

For a value that’s just read once a frame, there is no measurable performance difference.

The benefit of blueprints is that artists and designers can do much more within your game.

For a single indie developer, that doesn’t really matter, but for large teams (which is what Unreal’s main focus is,) this is super important.

Thus, making sure that your design is a good mix of C++ and Blueprint – heavy lifting in C++, art configuration in Blueprint – is probably the best way to become an Unreal game programmer, as the term is understood in the industry.

Anyway, there are many ways to do this, and you’re exploring some, and I’m suggesting some others. I’m sure there’s more! Find the one that matches your particular needs, and go with it. And don’t be afraid to rewrite and change things if you realize that another way will work better, later!