I have a simple AUnit actor which resembles a tank. I also have a ActorComponent ‘HealthComponent’ attached through it via code. The HealthComponent takes care of HP and should also spawn the health bar above the tank unit.
Relevant header of HealthComponent (UActorComponent):
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Health Bar", meta = (BindWidget) )
class TSubclassOf<class UHealthBar> HealthBarClass;
UPROPERTY(BlueprintReadOnly, Category = "Health Bar"/*)
class UHealthBar* HealthBarWidget;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Health Bar", meta = (BindWidget) )
class TSubclassOf<class UEntityHealthBar> EntityClass;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Health Bar"/*, meta = (BindWidget)*/ )
class UEntityHealthBar* EntityComp;
Relevant cpp of HealthComponent (UActorComponent):
UHealthComponent::UHealthComponent()
{
EntityComp = CreateDefaultSubobject<UEntityHealthBar>(TEXT("hp_widget_component"));
// This "HealthBarClass" variable is always NULL !!!
EntityComp->SetWidgetClass(HealthBarClass);
}
The variable “HealthBarClass” always seems to be NULL whenever this part of code is reached. Even if the unit blueprint i’ve created has set the “HealthBarClass” to the correct blueprint with the desired UI healthbar in it.
I’ve seen other examples online where people use some sort of helper function instead to find the “TSubclassOf” via a string address (example: “/ui/healthbar”). I thought this was messy and prone to errors in case i ever move that blueprint. So i want my HealthComponent to hold a reference to the UI blueprint. so that when i create the WidgetComponent for my unit actor, i can set the WidgetComponent’s Widget value to the stored blueprint.