Enemy healthbar is being shared by all enemies

This does seem like it’ll work but would it be simple to make it so that the hud is at the top of the players viewport instead of floating above each enemies head?

It’s doable, sure, but I haven’t seen this way in games. individual actor widget is more common in action and RPG games.

From the hovered actor, on hovered event “true” branch, create widget, save a variable for reference, and add to viewport
On unhovered event, check if the unhovered actor is equal to self, then get the variable reference to the widget, and call remove from parent.

1 Like

I’ll have to do some experimentation, maybe having normal health bars above the generic enemies and then the big health bar at the top for bosses

1 Like

I was using Diablo 3 as a refence, you can see it here how the hud changes depending on what’s hovered over. But might be too advanced for me. Thank you for all your help though!

Glad it helps. A single HUD can be tricky because of the fast pace of this game.
I’ll see if I can find anything and post here. Keep up the good work.

This is a quick test how this could be achieved.

  1. Created an abstract widget class that holds the default for child classes… Did mine in cpp for convinience. Here is a video I found helpful if you want to do it yourself: https://www.youtube.com/watch?v=T7v3UnL6PNU
.h
#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "HUDHealthBar.generated.h"

/**
 * 
 */
UCLASS(Abstract, Blueprintable)
class UHUDHealthBar : public UUserWidget
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintCallable, Category = "HUD Widget")
		void InitWidget(const float Health, const float MaxHealth, const FText &Name, const FText &Rank);
	UFUNCTION(BlueprintCallable, Category = "HUD Widget")
		void SetHealth(const float Health, const float MaxHealth);

private:
	UPROPERTY(EditAnywhere, meta = (BindWidget))
		class UProgressBar* HealthBar = nullptr;
	UPROPERTY(EditAnywhere, meta = (BindWidget))
		class UTextBlock* EnemyName = nullptr;
	UPROPERTY(EditAnywhere, meta = (BindWidget))
		class UTextBlock* EnemyRank = nullptr;
};
.cpp
#include "HUD/HUDHealthBar.h"
#include "Components/ProgressBar.h"
#include "Components/TextBlock.h"

void UHUDHealthBar::InitWidget(const float Health, const float MaxHealth, const FText &Name, const FText &Rank)
{
	if (HealthBar && EnemyName && EnemyRank)
	{
		SetHealth(Health, MaxHealth);
		EnemyName->SetText(Name);
		EnemyRank->SetText(Rank);
	}
}

void UHUDHealthBar::SetHealth(const float Health, const float MaxHealth)
{
	HealthBar->SetPercent(Health / MaxHealth);
}
  1. Next we creat a child class and place all the elements:


    I created 3 child widgets and changed their color just to test calling and child class works:
    image

  2. We create a struct that will hold the data for the HUD.:
    image

  3. Now that the struct is ready, we have to add it to the pawns:
    image
    You can manualy test them out if you wish:

  4. Then we need to set the logic to show on screen when mouse hovers over:


Result:
HUDEnemy

I’m sure this could be improved. Another option would be to have the hovered actor communicate with a HUD class that handles the widget switching.

Hope it at least serves as a starting point.

2 Likes

How are you moving the mouse cursor like this - it’s spooky. Looks as if you set Win’s mouse acceleration slider to max… Blasphemy! :innocent:

2 Likes

Thank you so much for all the time you put into this! I’m trying to follow along now but I’m a little slow. I’m trying to make it in BPs as I don’t know how to code and I’m struggling to make a child actor from a Widget Blueprint

It’s ok. It is hard at first, just keep at it.

Inheriting from a blueprint widget can be a bit more work, but here is more or less how it goes:

  1. Create your BP from User Widget class


    Set to Abstract:
    image
    This is to help us remember this class is to only be used to create children.
    Finally add the elements in the Designer Tab:

  2. Next create functions to set the data:



    image
    This last on is just so every child widget can have a different color.

  3. Create child widget and modify them (change the progress bar colors):
    image

There rest is the same as above… but I went a bit further and set up a Data Table to make things easier to manage:

  1. Using the struct above we create a Data table and fill out the rows with the info we want:

  2. We need an enum to make our lives even easier. Display Name should be same as Data Table row name:

  3. In our Pawn parent class, we create an enum variable and use it to get the row of the Data Table in begin play to set the struct:


    So now the HUD that will be added will be what is set in the enum:
    image

This helps a lot with consistency using the data through the data table + enum.

Hope it helps.


Here is further material if you are interested:

2 Likes

Thank you so much! You’re super helpful!!!

1 Like