Life Bar

Hello !

I have a question about how to create life bars for each enemy.

I created many enemies and they all have a healt variable. When I shoot at an ennemy his life decrease but not the life from the other enemies and that is perfect.
For the moment I display the life of the enemies with this :
Condition : The cursor is on the enemy
Action : Do =Enemy.VariableString(EnemyLife) to the text EnemyLifeText

The problem is this will only display the enemy that I’m hovering.
How do I create a lifebar that will follow the enemy and display the life of this precise enemy and not an other ?

Thanks for your help!

You have to find a condition that only that enemy fulfills :slight_smile:

A way to keep track specific instances is through a variable “ID” (different for each instance).
For example, if you want to display the life of the last damaged enemy, when the enemy gets damaged save its “ID” in a scene variable “LastDamagedEnemy”, then you can check the “ID” of the enemies:

Conditions: Enemy gets damaged Actions: Do -10 the variable Health of Enemy , Do = Enemy.Variable(ID) the variable LastDamagedEnemy

Conditions: Variable ID of Enemy is = Variable(LastDamagedEnemy) Actions: Put the life bar over Enemy , Set the width of the life bar = Enemy.Variable(Health)

Of course instead the condition of “Enemy gets damaged” you can set any condition (cursor over enemy, enemy selected, etc.), let me know if this is not really helpful :slight_smile:

Thanks for your answer but it’s not exactly what I want sorry :confused:

I would like to always display the life bar over the enemies at the same time.

I linked an image to show what I am trying to achieve.

Thank you for helping !

Hm the image doesn’t appear so I try again

I tried something like that :

Condition :
At the begining of the scene : Create objet LifeBar at position Enemy.PointX(LifeBar);Enemy.PointY(LifeBar)

But I have 2 problem, it only create a lifebar for only one enemy and the lifebar isn’t following the movement of the enemy.

And how do you do that thing : put the life bar over enemy.

And I’m not really sure to understand how to set the size of lifebar.

Here is what I did :

you want the red life bar to be a sprite then the green life bar to be a tiled sprite.

then you would add

Action - Create object RedLifeBar at position Enemy.PointX(LifeBar);Enemy.PointY(LifeBar) Action - Create object GreenLifeBar at position Enemy.PointX(LifeBar);Enemy.PointY(LifeBar)

Event 2

Action - Do= enemy.variable(enemy health) to the width of green life bar

Event 3

Condition - variable enemy health of enemy <=0 Action - do= 0 to variable enemy health of enemy

that should work not had time to test it but i have a health bar that is static and it works so hopefully that will follow your enemys, this will only work if your health bar is the same size in px as the maximum health of the enemy, if not you can probably implement it so instead of it taking the health off as a whole number it is done by a percentage so 100% enemy health will equal the width of your health bar but never tried doing anything like that yet.

Wow thanks for the help ! It works well !

The only problem left is that I still only have one LifeBar displayed.
It only show the health of one enemy and when I kill it it’ll show the lifebar of an other one.
How can I display the health of all the enemy at the same time ?

if you click on an event and select other, in that list there is a selection called for each object add that as an event and then copy and paste the creation of the lifebar into the events section under the repeat for each object event also add the zorder to it and it should create a life bar for each enemy.

so it will be

[code]for each object enemy, repeat

Actions - create red health bar and green health bar and set z-order so you can see enenemys health[/code]

Hm it’s still not working as I want.
If I only do create the lifebar, without moving it’s position after, it’s creating a lifebar each frame and it gets really messy.
If I use : do = Enemy.PointX(LifeBar);Enemy.PointY(LifeBar) to the position of lifebar. Then it only show one lifebar.
Here is the code I use. I’m only working with a red lifebar atm but it should be same.

Is there a way to avoid the creation of the lifebar every frame but to do it only once and then the lifebar follow the enemy ?

hmmm ill have a look when i finish work see what i can muster up :slight_smile:

You need to use the “Linked objects” extension to be able to (virtually) link an EnemyLifeBar with an Enemy.
Then, you can retrieve the EnemyLifeBar associated to a specific Enemy using a condition from this extension.

Thanks !

Sorry for bothering you but I tried many things and i can’t make it work :confused:

So, it works now or not ?

No I’m still stuck with the code I show up there :confused:

It should work with the code I posted yesterday.

Oh Sorry, for some reason the page bugged and I couldn’t see your post.

It works well now !

Thank you !

There is just one thing left.

When I kill the enemy the life isn’t destroyed. So if the life bar was having 10hp left and i kill him with 20 damages, then the 10hp lifebar will still appear on screen.
I try to do : Variable EnemyLife <=0 / Delet object LifeBar.
But then it destroy the life bar of all the enemies.
How can I change that ?

You need to have the same condition as in the For each event in my screenshot. It will take into account only the life bar associated with the enemy.
When you add an new enemy in-game, you also need to create a life bar and associate it with the newly created enemy (like in the first event of my post).

That’s what I used. And the lifebars are working.
But it’s just when the enemies dies, there is still what’s left of the lifebar showing.
But if I do : Delete EnemyLifebar
Then all the lifebars will be deleted. And I tried to put for each enemy but it didn’t worked.

Here is what I have :

You delete the Enemies with Life <= 0 in the event 58, so there are no Enemies left for events 59 and 60, that is why the lifebar is not deleted. The event 64 doesn’t run too (again, because the Enemy linked to the bar is deleted), and the lifebar of deleted Enemies is not updated :slight_smile:

The events 58, 59 and 60 should be merged in one event, they have the same condition. By the way, you forgot to add the condition “Take into account all the Red/BlackLifebar linked to Enemy” before delete the bars :wink:
The final event should be something like:

Conditions: Variable EnemyLife of Enemy is <= 0 Take into account all the RedLifebar linked to Enemy Take into account all the BlackLifebar linked to Enemy Actions: Delete object Enemy Delete object RedLifebar Delete object BlackLifebar
Maybe you have to put this event inside a “For each Enemy” event, I’m not sure :confused:

Great it works perfect now !

Thanks everyone for helping me out !!!

Thanks a lot !