Keep health bar value between scenes

Hi! I am wondering how I can keep the value of my health bar between scenes. I have it as a global number variable with a value of ten (since that’s what I want my health to be) but when I switch scenes it just resets back to ten. Anything helps! I’m new so try to explain it in dummy terms lol

Hi, at the beginning of your second scene you are setting your global variable “health” to the value of the health points of the health-extension (the value is probably 10). You have to do it the other way around, set the health points to the value of your global variable instead.

Something like:

I changed some stuff around but it still resets!

I was about to post example events here and then I realized what probably the problem is.

For scene 2 you have two blocks ‘At the beginning of the scene’-events. In the second one you create the player (the object with the health points) but you set the health points in the first event before the object is actually created - the sequence in the event sheets is always from top to bottom, which is also the case inside the event blocks.

So it should work if you put your create player-object - action in the the first block before setting the health points to the value of the global variable (actually you don’t need two ‘At the beginning of the scene’ -events anyway).

This is what I currently have and it still is resetting the health. This is with everything being in one beginning of scene event

Alright, here are my events. I can damage my player with a left mouse click and change the value of my global variable “player_health”. If I collide with a portal-sprite, I go to scene 2. There is also a text-object that displays the value of my global variable ‘player-health’.

In scene 2 I create the player-object and set its health points to the value of the global variable. I display the value in a text-object and can damage my player with a left mouse-click.

Everything works as expected.

When you test, do you go from scene 1 to scene 2? How do you know the value of your global variable?

Yes, I collide with a ghost sprite on scene 1 which damages me and then go to scene 2 and thats when the health resets. As far as having text for the global variable so I know if it changed, I dont have that set up yet lol

Okay! I got it! I added text to display the global variable and I can confirm it stayed the same between scenes. I just need the health bar to reflect that.

Edit: I got the health bar to carry over between scene 1 to 2, but when i go back to one, the health bar resets.

For scene 1 in the “At the beginning of the scene”-event: you need to change the health of player to your global health variable and the same for the health bar. You also need to set the initial value of your global variable (10). You can do that in the Project Manager → Global Variables. When you start with scene 1 everything will be set to 10 and when you come back from scene 2 the changed value will be displayed.