How would you monitor and change an image in a HUD widget? For example I have a health system similar to Skyward sword’s stamina meter(It doesn’t decrease like that, it takes chunks away like Zelda hearts.)

I have an image for each stage of health starting at 8 and going all the way down to 0. The idea was to change the widget image each time damage is taken and replace it with the new image but I’m unfortunately having trouble figuring it out.
UObject* txture = StaticLoadObject(UObject::StaticClass(), nullptr, *FString("Texture2D'/Game/UI/Textures/img01.img01'"));
if (txture)
{
UTexture2D* texture = Cast<UTexture2D>(txture);
if (texture)
{
}
}
this should load an image for you.
for 5 images you could load them and store them in an array
TArray<UTexture2D*> images;
Then when you want to get a different image you can store and use an indexInteger like
int currentImageIndex = 2;
images[currentImageIndex ]
if you have an Image with a material set you can use
UMaterialInstanceDynamic* mID = buttonImage->GetDynamicMaterial();
if (mID)
{
mID->SetTextureParameterValue("texture", images[currentImageIndex ]);
}
to set the wanted image in the material (which is used in the UMG I assume)
So basically load the 8 images you need and store them in an array. Then when your health changes and is 5 for example, you’d use images[5]
to get the 5th image in the array
ensure that the Image object in the Widget is marked as “IsVariable = True
”
then to modify the image being displayed you would take the MyImage
reference and call SetBrush
for swapping between specific images you can hold the images in an Array, and then perform a GetElement on the Array for the specific image, or if you want to be extremely specific, skipping the Array, you can feed the HealthValue
as the variable input of a Select Node.
though I would maybe suggest kind of against this approach kind of, images add up and in widgets they will all be active, and it will depend on the resolution of these images, and if you need multiple versions depending on display resolution.
That being said you can have your background be an image, but then have a “Circular Progress bar” here is an example
to achieve the “chunk” display effect:
-before applying the value being fed in to the “progress bar” you would do your chunk value constraint, and then only apply that
if this Health is an Int (realistically something as tangible as health should be an int anyways) then when you feed it into the progress bar get the % health as a float (because progress bars require a float as an input) with proportions
//in most cases %Max is 100 and "Min" is 0
%Current = (%Max(Current-Min))/(Max-Min)
this way you don’t need 9 images to represent 8 health points.