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...
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.
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.
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!