# How to divide a health variable by same amount of itself when it is constantly changing ?

I have a health bar enemy widget over the enemies. The health variable changes for each new wave when enemy monster respawn and they get additional health. So basicly to make the widget health bar move accordingly to the damage i do it has to be divided by the same amount the health currently is. For example 200 health has to be divided by 200 then then the bar moves with each damage hit and when the next wave starts and the health amount is for example 250 so then again it needs to be divided by 250 and so on.
It looks like this

This example works only for the first wave when the health is 200 as well but after the wave the health increase and the divide by 200 isnt working anymore.
So i tought i may just need to get the "life amount and divide it by itself and hook up the âlife amountâ there instead of 200. But then the health bar just stops moving

The âLife amountâ is comming from a simple Interface which updates correctly the new values (on the right you see that it has value 0 but it acctually updates in the moment i shoot and hit an enemy and sets it correctly to 200 for example and maybe thats why "life amount divided by âlife amountâ isnt working and it kinda thinks is 0 on this one) So now im not sure how i can make this working any tip how to solve this would be greatly appreciated.

1 Like

You need two variables. One is the total life, the other is how much life is left.

So total_life = 250, and remaining = 85

Widget is 85/250.

With your method, it will always be 1

1 Like

can you give any more detail how i can do this or any tutorial where i can see how its done ?

I just tried to make the variable life remaining in my enemy BP where the dmg logic ist with a print string and it shows me the correct numbers with each hit life remaining is looking good as expected i do 10 dmg so it go from 250 then 240 230 and so on so must be correct i guess

But then i should not divide that with the life remaining right? if i try to divide it the bar dissapiers i would need some other logic for it hmm.

Ok so 85/250 means i need to reverse divide the remaining life divided the life amount but for some reason my health widget dont show up i guess my casting from the enemy reference variable is false there ? if i print string it it shows me 0 so that must be it

I think itâs just these two, the other way around

You have to divide the amount remaining by the total amount it can have.

1 Like

If i do this the widget dissapiers i think the casting is false the reference to the enemy isnt working i think and print string shows me value of 0 when i hook the life remaining variable to it

am i getting it wrong here or should i cast first to this enemy then get the âhow much life leftâ variable from it ?

Iâm assuming youâre doing this from the player?

Then yes, the reference needs to be valid

If you donât have a valid ref yet, then donât show anything.

I donât get it though, surely the life amount is also in the enemy?

Yes it is in the enemy and i made an Interface for it

No this is in the widget itself on the progress bar then âbindâ function it may be better to call this on the player right ? I mean more efficient for performance ? and should i try some thing like this maybe ?

This looks ok, from what I can see, but you need floats all the way. Otherwise youâre getting integer division, thatâs not going to help.

Using a bind in the widget is absolutely fine for now.

In the long run, or if you have a lot of enemies, then you need to set this from the enemy.

It doesnât make sense to set it from the player, they shouldnât know about enemy damage. They just fire a missile ( or whatever ) and do some damage, but donât know how much until they see the widget.

I cant make it work

I guess reference is still not right like this print string dont show up anymore and healthbar is awso gone. Then i tried to make this in the enmy BP and unplug the function in the healthbar from the screenshot above and then i used this

But then healthbar is awso gone

That was the first thing i did as well then i switched to sequence. When i plug it like that or with sequence the healthbar just dissapiers. when i plug in the how much life left node to print string it shows me 0 so thats why the bar is gone at 0 its invisibleâŚ Not sure why i cant read the correct value of this how much life left. i even made an interface to it so i can read the value without the need of castingâŚ Something isnt right as it seems at the enemy BP with this variable

Like here where it is plugged in it reads the correct life left values but only the connected node. I just changed the default value of it to be 300 and now the top screenshot works and the healthbar shows again so befor that it was to 0 value. how can i get the reference to the updated node âhow much life leftâ node which is the connected one top screenshot ? So short version setting this default value does not help i need to get this to aways update with the new values.

I made 2 interfaces just for this 2 values and the wierd thing is the âLife Amountâ variable works perfectly fine like this and aways update the correct life amount but i cant replicate the same with the âhow much life leftâ node

Do you maybe know how i can abandon the bind function and move this directly to the eneemy BP with this nodes set percent ?

If i can do that i can directly connect the updated life left and current life nodes so like this it should work. Unfortunatly the widget do not show up when i try to connect this âSet percent nodesâ right after event beginn play sadlyâŚ Acctually it is there but empty at 0 and is still not showing the red filling color nor is anything moving when hitting an enemyâŚ

Ok, letâs take a step back.

You have a player, an enemy and a widget.

1. Do you create the widget in the player or in the enemy, where is the create widget call?

2. How does the player damage the enemy?

Its on the enemy i followed this guide https://www.youtube.com/watch?v=BpTKS5-9KnE

The player does damage with simple damage interface

i add the damage for example here on my main attack

and here is the widget on the enemy and it has external widget BP for it

So after following the health widget guide above the enemyâs head it works flawlessly just like on the tutorial problem is he awso has fixed amount of life i think 100 and then he divides that by 100 so progressbar moves as it should. This is the case for me too but the life increases of my enemyâs so this works only for the first wave of monster spawn. Basicly all i need is to change this like you showed me but i cant get the variables ref to them correctly in the widget BP when i try to get them from the enemy.
If i cant do that i should try to move the widget progress bar not calling it from the âbindâ function on the widget but on the enemy where the correct variables are and updating correctly i think if i can make it to show up from there it may work.

Cool, I will come back in a little while.

1 Like

Have to mention that those current life variable changes in the spawner i have in the moment when the next wave spawns. So i made extra an interface just for the current life variable so the spawner can get access to it as well as the enemy BP where my dmg was and the widgetBP. And that works as it seems after each wave the current life increases accordingly. But now i made an extra interface for the second variable the âhow much life is leftâ and this one seems to not work as it should not sure why or how to access it properly in the widgetBP.

Well, very strange, I canât get a widget component to work - AT ALL.

Last time I tried was 4.x or 5.0, but it was quite a while ago.

Canât even get it to work with a bind!

1 Like

This doesnât sound good. I wasted yestarday the whole day working on it but in the end i still couldnât fix it Not sure what to do maybe i should move on for now and try again later. Anyway thx for the help and let me know if you do find something later. Iâm using 5.1 btw

But the widget component should acctually work did you try maybe the same tutorial i posted above ? It works for me just like there but i additionally need to change this 1 variable to be aways up to date is the only difference and the only thing that did not work for me. Widget itself get shown correctly and the health bar moves according to the damage i do, but like i say only when the value is fixed tough on the dividing equation partâŚ

I will post later if i find some solution to this

This is what I have, which should work

In the player

In the enemy ( here 3rd person )

You could use another interface here, but thereâs no point, because you have already declared the widget type in the widget component. So itâs âuncleanâ anyway.

Try this, maybe it works in 5.1

@Everynone : Do you have 5.2? This should work, but doesnâtâŚ :-/ ( just checking Iâm not going mad ).

1 Like

Seem to work under 5.2:

Which part of this fails?!

@catz111 the engine already has a damage interface with more bells & whistles one could wish for. Consider using it rather than developing your own.

1 Like