# Life system

Hello,
I started a small platformer and I want my character to start with 3 hearts (lifes) and each time he is hit by enemy, he loses one, until he dies. Does somebody knows how to do it?
I created an animation with 3 hearts, then 2, then one, this animation responds to “when player is in colission with enemy”, but i don’t get the results i want.
If somebody is strong at this, it will be more than supercool to hear your ideas.
Thanks and good games,
charpi.

What I would do is, create 4 animation for the hearts:
Animation 0 = 0 heart
Animation 1 = 1 heart
Animation 2 = 2 heart
Animation 3 = 3 heart

After I would use an object variable for the player to store how many lifes the player got. You can do this in events but also in object properties. The initial value of this variable would be 3 and I would use this variable to display the correct animation for the hearts by using an expression:

``` Do = player.Variable(life) to the Animation of hearts```

And I would reduce the value of this variable by 1 each time the player is in collision with the enemy

``` If player is in collision with enemy : Do - 1 to the variable life of the player object Trigger Once```

As a result I would get:

If life = 3 the animation of hearts would be 3 (3 hearts)
If life = 2 the animation of hearts would be 2 (2 hearts)
If life = 1 the animation of hearts would be 1 (1 hearts)
If life = 0 the animation of hearts would be 0 (0 hearts)

So basically, a single variable would be used for the life of the player and also for the animation number of the hearts object so whenever the life of the player changes, the animation of hearts would also change with it and display the actual state of the player life.

Thanks, u rock. I think the key is variables, and im gonna try what you said.

I’m still having same problem (when player is in collision with ennemy, he loses 2 hearts instead on one). But i think i made a step forward.

Events are triggered (ideally) 60 times per second, you need to make sure the event to reduce life by 1 on collision with enemy, is Triggered only Once!

The whole thing should look something like this:

I haven’t tested but it should work If I’m not missing something. Let me know if ti doesn’t work.

I have just edited my previous post as well as it was misleading how to use Trigger Once condition as it need to be the LAST condition in the event you want to be triggered only once.

Oh yes, this looks good and quite close to what i was trying.
Thanks again, I will try it. Can’t wait to try it

This is getting really weird…; This sh+t is too hard for me.

i’m running gdevelop on a mac, and it just bugs and crashes all the time. i may buy a pc and start all over

It is simple really. I have just created an example for you:
life_system.zip (101 KB)

In case you are using the Mac version, I could be wrong but I guess it able to export only HTML5, if that is the case and you are preview the project in HTML5, make sure you run a hard refresh between each preview because the web browsers likes to use stored version of webpages to speed loading time so when you run preview, your browser may decide to load the previous version instead of the new version and makes you think it doesn’t work what you have just done, but really it is your browser. So do a hard refresh in your browser to make sure the actual changes are loaded.

Wow, going to see this right now^^
Well, i think i will do the life system right, but i have objects that disappear without any reason, etc. these are pure gdevelop bugs.

Preview of your amazing exemple wont open, but i can see the code, and dude… this looks fantastic.

The preview may not open because it is a native project, and I believe the Mac version doesn’t support native games right now. Really I was expecting the Mac version may switch to HTML5 by default if native is not supported…

So, if the preview doesn’t work on your side, go to the extensions and on the top just click on HTML5 to activate it, after close the scene if it is open and re-open the scene and you should be able to run HTML5 preview.
I have tested and it also works in HTML5 on my side.

Hard to believe, many years working/testing/trying to crash GDevelop, and the times I’ve found buggy behaviours as yours, it was my fault (complex events leading to illogical actions order)