[Solved] Hearts for health: how do I simplify this code?

I’m trying to make multiple hearts display for the players health. When the player takes damage 1 heart is removed. I’ve got a working code for it, but I’m pretty sure there’s an easier way to do this. How can I simplify this?

How about if you use 1 object HeartLifePoint and put an object variable “life” where in the first heart it will equal to 1, in the second 2 and in the third 3.

Then do a for each object HeartLifePoint when the Variable PlayerHealthBar < HeartLifePoint.life do 0 to opacity of HeartLifePoint.

That way if your character can get heath pickups you just put another for each object HeartLifePoint when the Variable PlayerHealthBar >= HeartLifePoint.life do 255 to opacity of HeartLifePoint and they will show up again

1 Like

Thanks, but could you elaborate in more detail? I’m really new to this and am still learning.

What if you create a special layer for your energy bar and place the hearts, score, items, etc. on that layer. In this way you don’t have to create them as an event and the code is shorter.
http://wiki.compilgames.net/doku.php/gdevelop5/interface/scene-editor/layers-and-cameras

What MeX1Co tells you is also a good method.

this code is totally wrong, even if it seems to work. but I saw in your another post, this is no longer current.

I will make you an example later tonight and upload it here…

Here it is… If you click on the minus button the health will decrease and on the plus button it will increase… The code for the hiding hearts is the two “for each objects”… Hope it helps…

https://drive.google.com/file/d/1gdh8TBtkeJE4omjFN71XEbvjZXJTj9wS/view?usp=sharing

I have a tutorial on that.

1 Like

That was perfect thanks for that.

you are welcome, am always here if you have any question